OpenBarnyard
 
Loading...
Searching...
No Matches
TRenderInterface_DX8.cpp
Go to the documentation of this file.
1// DirectX 8 Render Interface Implementation
2// This file contains the implementation of the DirectX 8 render interface for the Toshi engine
3
4#include "ToshiPCH.h"
5#include "TModel_DX8.h"
12
13#include "Render/TShader.h"
14
15#include <dxerr8.h>
16#pragma comment( lib, "DxErr8.lib" )
17
18//-----------------------------------------------------------------------------
19// Enables memory debugging.
20// Note: Should be the last include!
21//-----------------------------------------------------------------------------
22#include "Core/TMemoryDebugOn.h"
23
25
27
28// $Barnyard: FUNCTION 006c58e0
29// Tests if the device can begin and end a scene successfully
31{
32 auto pHAL = TSTATICCAST( TRenderD3DInterface, this );
33
34 if ( S_OK == pHAL->GetDirect3DDevice()->BeginScene() )
35 {
36 pHAL->GetDirect3DDevice()->EndScene();
37 }
38}
39
40// $Barnyard: FUNCTION 006c6880
41// Constructor - Initializes DirectX 8 render interface with default values
43{
44 m_pDirect3D = TNULL;
45 m_pDirectDevice = TNULL;
46 m_fPixelAspectRatio = 1.0f;
47 m_AcceleratorTable = NULL;
48 m_pDevice = TNULL;
49 m_oDisplayParams.uiWidth = 640; // Default width
50 m_oDisplayParams.uiHeight = 480; // Default height
51 m_oDisplayParams.uiColourDepth = 32; // Default color depth
52 m_oDisplayParams.eDepthStencilFormat = 0;
53 m_oDisplayParams.bWindowed = TTRUE;
54 m_fBrightness = 0.5f; // Default brightness
55 m_fSaturate = 0.5f; // Default saturation
56 m_bExited = TFALSE;
57 m_bCheckedCapableColourCorrection = TFALSE;
58 m_bCapableColourCorrection = TFALSE;
59 m_bFailed = TFALSE;
60 m_Unk1 = TNULL;
61 m_Unk2 = TNULL;
62 m_fContrast = 0.583012f; // Default contrast
63 m_fGamma = 0.420849f; // Default gamma
64 m_bChangedColourSettings = TTRUE;
65 m_bEnableColourCorrection = TTRUE;
66}
67
68// $Barnyard: FUNCTION 006c6da0
69// Destructor - Cleans up DirectX resources
74
75// $Barnyard: FUNCTION 006c6990
76// Creates the display with specified parameters
77// Returns true if successful, false otherwise
79{
81 {
83 return TFALSE;
84 }
85
86 // Find appropriate device for the display parameters
87 m_pDevice = TSTATICCAST( TD3DAdapter::Mode::Device, FindDevice( a_rParams ) );
88 m_oDisplayParams = a_rParams;
89
90 if ( m_pDevice )
91 {
92 auto pDisplayParams = GetCurrentDisplayParams();
93
94 // Get desktop window dimensions
95 RECT clientRect;
96 GetClientRect( GetDesktopWindow(), &clientRect );
97
98 // Handle large displays
99 if ( 2000 < clientRect.right )
100 {
101 clientRect.right /= 2;
102 }
103
104 TUINT32 uiWindowPosX = 0;
105 TUINT32 uiWindowPosY = 0;
106
107 // Calculate window position for windowed mode
108 if ( pDisplayParams->bWindowed )
109 {
110 auto pMode = GetCurrentDevice()->GetMode();
111 uiWindowPosX = ( clientRect.right - pMode->GetWidth() ) / 2;
112 uiWindowPosY = ( clientRect.bottom - pMode->GetHeight() ) / 2;
113 }
114
115 // Initialize presentation parameters
116 TUtil::MemClear( &m_PresentParams, sizeof( m_PresentParams ) );
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();
123 m_PresentParams.AutoDepthStencilFormat = TD3DAdapter::Mode::Device::DEPTHSTENCILFORMATS[ pDisplayParams->eDepthStencilFormat ];
124 m_PresentParams.BackBufferWidth = pDisplayParams->uiWidth;
125 m_PresentParams.BackBufferHeight = pDisplayParams->uiHeight;
126
127 // Get device information
129 auto pMode = TSTATICCAST( TD3DAdapter::Mode, pDevice->GetMode() );
130 auto pAdapter = TSTATICCAST( TD3DAdapter, pMode->GetAdapter() );
131 auto uiAdapterIndex = pAdapter->GetAdapterIndex();
132
133 // Set back buffer format based on windowed/fullscreen mode
134 if ( pDisplayParams->bWindowed )
135 {
136 m_PresentParams.BackBufferFormat = pMode->GetD3DDisplayMode().Format;
137 }
138 else
139 {
140 m_PresentParams.BackBufferFormat = pMode->GetBackBufferFormat( pDisplayParams->uiColourDepth );
141 }
142
143 // Create Direct3D device
144 HRESULT hRes = m_pDirect3D->CreateDevice(
145 uiAdapterIndex,
146 TD3DAdapter::Mode::Device::DEVICETYPES[ pDevice->GetDeviceIndex() ],
147 m_Window.GetHWND(),
148 pDevice->GetD3DDeviceFlags(),
149 &m_PresentParams,
150 &m_pDirectDevice
151 );
152
153 if ( FAILED( hRes ) )
154 {
156 PrintError( hRes, "Failed to create D3D Device!" );
157 return TFALSE;
158 }
159
160 // Initialize device states
162
163 // Set window mode
164 if ( pDisplayParams->bWindowed )
165 {
166 m_Window.SetWindowed();
167 }
168 else
169 {
170 m_Window.SetFullscreen();
171 }
172
173 // Handle multi-monitor setup
174 if ( uiAdapterIndex != 0 )
175 {
176 HMONITOR hMonitor = m_pDirect3D->GetAdapterMonitor( uiAdapterIndex );
177
178 MONITORINFO monitorInfo = { .cbSize = sizeof( monitorInfo ) };
179 GetMonitorInfoA( hMonitor, &monitorInfo );
180
181 uiWindowPosX += monitorInfo.rcMonitor.left;
182 uiWindowPosY += monitorInfo.rcMonitor.right;
183 }
184
185 // Set window position and size
186 m_Window.SetPosition( uiWindowPosX, uiWindowPosY, pDisplayParams->uiWidth, pDisplayParams->uiHeight );
187
188 // Get back buffer surface description
189 IDirect3DSurface8* pSurface;
190 m_pDirectDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pSurface );
191 pSurface->GetDesc( &m_SurfaceDesk );
192 pSurface->Release();
193
194 // Set cursor position to center of window
195 SetCursorPos(
196 uiWindowPosX + pDisplayParams->uiWidth / 2,
197 uiWindowPosY + pDisplayParams->uiHeight / 2
198 );
199
200 m_pDirectDevice->ShowCursor( TRUE );
201
202 // Create invalid texture pattern
203 TUINT invalidTextureData[ 32 ];
204 for ( TINT i = 0; i < 32; i++ )
205 {
206 invalidTextureData[ i ] = 0xff0fff0f;
207 }
208
210 m_pInvalidTexture = pTextureFactory->CreateTextureFromMemory( invalidTextureData, sizeof( invalidTextureData ), 0x11, 8, 8 );
211
212 // Enable color correction and mark display as created
215
216 return TTRUE;
217 }
218
220 return TFALSE;
221}
222
223// $Barnyard: FUNCTION 006c6520
225{
228
229 if ( IsDisplayCreated() )
230 {
231 if ( m_pDirectDevice )
232 {
233 m_pDirectDevice->Release();
234 }
235
237 return TTRUE;
238 }
239 else
240 {
241 return TTRUE;
242 }
243}
244
245// $Barnyard: FUNCTION 006c6560
247{
249 m_Window.Update( a_fDeltaTime );
250
251 return !m_bExited;
252}
253
254// $Barnyard: FUNCTION 006c6590
256{
257 TASSERT( !IsInScene() );
258
260 {
261 HRESULT hRes = m_pDirectDevice->BeginScene();
262
263 if ( SUCCEEDED( hRes ) )
264 {
265 RECT windowRect;
266 GetWindowRect( m_Window.GetHWND(), &windowRect );
267
268 D3DVIEWPORT8 viewport = {
269 .X = 0,
270 .Y = 0,
271 .Width = DWORD( windowRect.right - windowRect.left ),
272 .Height = DWORD( windowRect.bottom - windowRect.top ),
273 .MinZ = 0.0f,
274 .MaxZ = 1.0f,
275 };
276
277 m_pDirectDevice->SetViewport( &viewport );
279
281 m_BeginSceneEmitter.Throw( 0 );
282 }
283 else
284 {
285 return TFALSE;
286 }
287 }
288
289 return TTRUE;
290}
291
292// $Barnyard: FUNCTION 006c6fd0
294{
295 TASSERT( IsInScene() );
296
297 if ( IsInScene() )
298 {
299 m_EndSceneEmitter.Throw( TNULL );
300 HRESULT hRes = m_pDirectDevice->EndScene();
301
302 if ( SUCCEEDED( hRes ) )
303 {
304 HRESULT hPresentRes = m_pDirectDevice->Present( NULL, NULL, NULL, NULL );
305
306 if ( hPresentRes == D3DERR_DEVICELOST )
307 {
308 HRESULT hCooperativeLevel = m_pDirectDevice->TestCooperativeLevel();
309
310 if ( hCooperativeLevel == D3DERR_DEVICELOST )
311 {
312 OnD3DDeviceLost();
313 }
314 else if ( hCooperativeLevel == D3DERR_DEVICENOTRESET )
315 {
316 OnD3DDeviceFound();
317 }
318 }
319
321 }
322 else
323 {
324 return TFALSE;
325 }
326 }
327
328 return TTRUE;
329}
330
331// $Barnyard: FUNCTION 006c5950
336
337// $Barnyard: FUNCTION 006c5960
342
343// $Barnyard: FUNCTION 006c7310
345{
346 return Create( TClassObjectName.GetName() );
347}
348
349// $Barnyard: FUNCTION 006be990
351{
353
354 for ( auto it = TShader::sm_oShaderList.GetRootShader(); it != TNULL; it = it->GetNextShader() )
355 {
356 it->Flush();
357 }
358}
359
360TBOOL TRenderD3DInterface::CreateVertexShader( const DWORD* a_ShaderDeclaration, const DWORD* a_pFunction, DWORD* a_pOutVertexShader )
361{
362 auto pD3DDevice = Interface()->GetDirect3DDevice();
363 auto pCurrentDevice = Interface()->GetCurrentDevice();
364 auto bSupportsHardwareTransformations = pCurrentDevice->SupportsHardwareTransfomations();
365
366 HRESULT hRes = pD3DDevice->CreateVertexShader(
367 a_ShaderDeclaration,
368 a_pFunction,
369 a_pOutVertexShader,
370 !bSupportsHardwareTransformations ? D3DCREATE_PUREDEVICE : 0
371 );
372
373 if ( FAILED( hRes ) )
374 {
375 PrintError( hRes, "Failure to create the vertex shader!" );
376 return TFALSE;
377 }
378
379 return TTRUE;
380}
381
382void TRenderD3DInterface::DestroyVertexShader( DWORD a_hVertexShader )
383{
384 auto pD3DDevice = Interface()->GetDirect3DDevice();
385 HRESULT hRes = pD3DDevice->DeleteVertexShader( a_hVertexShader );
386 TASSERT( SUCCEEDED( hRes ) );
387}
388
389TBOOL TRenderD3DInterface::CreatePixelShader( const DWORD* a_pFunction, DWORD* a_pOutPixelShader )
390{
391 auto pD3DDevice = Interface()->GetDirect3DDevice();
392 auto pCurrentDevice = Interface()->GetCurrentDevice();
393 auto bSupportsHardwareTransformations = pCurrentDevice->SupportsHardwareTransfomations();
394
395 HRESULT hRes = pD3DDevice->CreatePixelShader( a_pFunction, a_pOutPixelShader );
396
397 if ( FAILED( hRes ) )
398 {
399 PrintError( hRes, "Failure to create the pixel shader!" );
400 return TFALSE;
401 }
402
403 return TTRUE;
404}
405
406void TRenderD3DInterface::DestroyPixelShader( DWORD a_hPixelShader )
407{
408 auto pD3DDevice = Interface()->GetDirect3DDevice();
409 HRESULT hRes = pD3DDevice->DeletePixelShader( a_hPixelShader );
410 TASSERT( SUCCEEDED( hRes ) );
411}
412
413// $Barnyard: FUNCTION 006c5970
414void TRenderD3DInterface::PrintError( TINT32 a_eError, const TCHAR* a_szInfo )
415{
416 if ( !a_szInfo )
417 {
418 a_szInfo = "D3D Error";
419 }
420
421 const TCHAR* errString = GetErrorString( a_eError );
422 const TCHAR* errDescription = GetErrorDescription( a_eError );
423
424 TString8 string = TString8::VarArgs( "> %s: D3D Error [%s] : Description [%s] !\n", a_szInfo, errString, errDescription );
425 OutputDebugStringA( string );
426
427 TASSERT( TFALSE, string.GetString() );
428}
429
430// $Barnyard: FUNCTION 006c64a0
432{
433 TASSERT( TTRUE == IsCreated() );
434
435 if ( IsCreated() )
436 {
440
443
444 m_Window.UnregisterWindowClass();
446
447 if ( m_pDirect3D )
448 {
449 m_pDirect3D->Release();
450 m_pDirect3D = TNULL;
451 }
452
454 }
455 else
456 {
457 return TTRUE;
458 }
459}
460
461// $Barnyard: FUNCTION 006c5f10 TODO
462void TRenderD3DInterface::RenderIndexPrimitive( TINT param_2, TINT param_3, TINT param_4, TINT param_5, TINT param_6, TINT param_7 )
463{
464 TIMPLEMENT();
465}
466
467// $Barnyard: FUNCTION 006c57e0
469{
470 return m_fPixelAspectRatio;
471}
472
473// $Barnyard: FUNCTION 006c57f0
475{
476 m_fPixelAspectRatio = a_fPixelAspectRatio;
477 return TTRUE;
478}
479
480// $Barnyard: FUNCTION 006c67c0
482{
483 TASSERT( TTRUE == IsInScene() );
484
485 for ( auto it = m_OrderTables.Begin(); it != m_OrderTables.End(); it++ )
486 {
487 it->Flush();
488 }
489}
490
491// $Barnyard: FUNCTION 006c6030
496
497// $Barnyard: FUNCTION 006c6240
502
503// $Barnyard: FUNCTION 006c6260
505{
506 TTODO( "a_pRenderCapture->vftable + 8" );
507 delete a_pRenderCapture;
508}
509
510// $Barnyard: FUNCTION 006c6230
511void* TRenderD3DInterface::CreateUnknown( const TCHAR* a_szName, TINT a_iUnk1, TINT a_iUnk2, TINT a_iUnk3 )
512{
513 return TNULL;
514}
515
516// $Barnyard: FUNCTION 006c6280
518{
519 auto pModel = new TModelHAL();
520
521 if ( pModel )
522 {
523 if ( !pModel->Create( a_pTMD, a_bLoad ) )
524 {
525 pModel->Delete();
526 return TNULL;
527 }
528 }
529
530 return pModel;
531}
532
533// $Barnyard: FUNCTION 006c62d0
534TModel* TRenderD3DInterface::CreateModel( const TCHAR* a_szFilePath, TBOOL a_bLoad )
535{
536 auto pModel = new TModelHAL();
537
538 if ( pModel )
539 {
540 if ( !pModel->Create( a_szFilePath, a_bLoad ) )
541 {
542 pModel->Delete();
543 return TNULL;
544 }
545 }
546
547 return pModel;
548}
549
550// $Barnyard: FUNCTION 006c6320
551TModel* TRenderD3DInterface::CreateModel( const TCHAR* a_szFilePath, TBOOL a_bLoad, TTRB* a_pAssetTRB, TUINT8 a_ui8FileNameLen )
552{
553 auto pModel = new TModelHAL();
554
555 if ( pModel )
556 {
557 if ( !pModel->Create( a_szFilePath, a_bLoad, a_pAssetTRB, a_ui8FileNameLen ) )
558 {
559 pModel->Delete();
560 return TNULL;
561 }
562 }
563
564 return pModel;
565}
566
567// $Barnyard: FUNCTION 006c6420
573
574// $Barnyard: FUNCTION 006c6440
576{
577 if ( m_pDebugText )
578 {
579 delete m_pDebugText;
581 }
582}
583
584// $Barnyard: FUNCTION 006c6f80
586{
587 if ( IsCreated() )
588 {
589 if ( IsDisplayCreated() )
590 {
591 OnD3DDeviceLost();
593
594 if ( CreateDisplay( a_rDisplayParams ) )
595 {
596 OnD3DDeviceFound();
597 return TTRUE;
598 }
599 }
600 }
601
602 return TFALSE;
603}
604
605// $Barnyard: FUNCTION 006c5a00
607{
608 TMath::Clip( a_fConstrast, 0.0f, 1.0f );
609 m_fContrast = a_fConstrast;
610 m_bChangedColourSettings = TTRUE;
611}
612
613// $Barnyard: FUNCTION 006c5a30
615{
616 TMath::Clip( a_fBrightness, 0.0f, 1.0f );
617 m_fBrightness = a_fBrightness;
618 m_bChangedColourSettings = TTRUE;
619}
620
621// $Barnyard: FUNCTION 006c5a60
623{
624 TMath::Clip( a_fGamma, 0.0f, 1.0f );
625 m_fGamma = a_fGamma;
626 m_bChangedColourSettings = TTRUE;
627}
628
629// $Barnyard: FUNCTION 006c5a90
631{
632 TMath::Clip( a_fSaturate, 0.0f, 1.0f );
633 m_fSaturate = a_fSaturate;
634 m_bChangedColourSettings = TTRUE;
635}
636
637// $Barnyard: FUNCTION 006c6950
639{
640 return m_fContrast;
641}
642
643// $Barnyard: FUNCTION 006c6960
645{
646 return m_fBrightness;
647}
648
649// $Barnyard: FUNCTION 006c6970
651{
652 return m_fGamma;
653}
654
655// $Barnyard: FUNCTION 006c6980
657{
658 return m_fSaturate;
659}
660
661// $Barnyard: FUNCTION 006c66b0
663{
664 if ( IsColourCorrection() )
665 {
667 m_pDirectDevice->SetGammaRamp( 0, &m_GammaRamp );
668 }
669}
670
671// $Barnyard: FUNCTION 006c5e70
673{
674 if ( !m_bCheckedCapableColourCorrection )
675 {
676 D3DCAPS8 caps;
677 HRESULT hRes = m_pDirectDevice->GetDeviceCaps( &caps );
678 m_bCapableColourCorrection = SUCCEEDED( hRes ) && HASANYFLAG( caps.AdapterOrdinal, 0x20000 );
679 }
680
681 return m_bCapableColourCorrection;
682}
683
684// $Barnyard: FUNCTION 006c5ec0
686{
687 m_bEnableColourCorrection = a_bEnable && IsCapableColourCorrection();
688}
689
690// $Barnyard: FUNCTION 006c5ef0
692{
693 m_bEnableColourCorrection = a_bEnable;
694}
695
696// $Barnyard: FUNCTION 006c5f00
698{
699 return m_bEnableColourCorrection;
700}
701
702// $Barnyard: FUNCTION 006c6de0 TODO
703void TRenderD3DInterface::OnD3DDeviceLost()
704{
705 TIMPLEMENT();
706}
707
708// $Barnyard: FUNCTION 006c6e80 TODO
709void TRenderD3DInterface::OnD3DDeviceFound()
710{
711 TIMPLEMENT();
712}
713
714// $Barnyard: FUNCTION 006c5b60 TODO
716{
717 TIMPLEMENT();
718
719 if ( m_bChangedColourSettings )
720 {
721 m_bChangedColourSettings = TFALSE;
722 }
723}
724
725// $Barnyard: FUNCTION 006d9fd0 TODO
727{
728 TIMPLEMENT();
729 return TNULL;
730}
731
732// $Barnyard: FUNCTION 006c66e0
733// Sets default Direct3D device states
735{
736 // Enable Z-buffer
737 m_pDirectDevice->SetRenderState( D3DRS_ZENABLE, 1 );
738 // Set culling mode to counter-clockwise
739 m_pDirectDevice->SetRenderState( D3DRS_CULLMODE, 2 );
740 // Disable lighting
741 m_pDirectDevice->SetRenderState( D3DRS_LIGHTING, 0 );
742 // Set texture filtering to linear
743 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, 2 );
744 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, 2 );
745 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, 2 );
746}
747
748// $Barnyard: FUNCTION 006c6110
749// Sets texture addressing mode for a specific texture stage and coordinate
750void TRenderD3DInterface::SetTextureAddress( TINT a_iStage, ADDRESSINGMODE a_eAddressing, TEXCOORD a_eTextureCoordinate )
751{
752 // Map texture addressing type to D3D address mode
753 DWORD addressMode;
754 switch ( a_eAddressing )
755 {
757 addressMode = D3DTADDRESS_WRAP;
758 break;
760 addressMode = D3DTADDRESS_MIRROR;
761 break;
763 addressMode = D3DTADDRESS_CLAMP;
764 break;
766 addressMode = D3DTADDRESS_BORDER;
767 break;
768 default:
769 TERROR( "TRenderD3DInterface::SetTextureAddress: Invalid addressing mode specified!\n" );
770 return;
771 }
772
773 // Set either U or V coordinate addressing
774 if ( a_eTextureCoordinate == TEXCOORD_U )
775 {
776 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSU, addressMode );
777 }
778 else if ( a_eTextureCoordinate == TEXCOORD_V )
779 {
780 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSV, addressMode );
781 }
782 else
783 {
784 TASSERT( a_eTextureCoordinate == TEXCOORD_UV );
785 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSU, addressMode );
786 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSV, addressMode );
787 }
788}
789
790// $Barnyard: FUNCTION 006c6070
791// Clears a region of the screen with specified color, depth, and stencil values
792void TRenderD3DInterface::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 )
793{
794 // Build clear flags based on parameters
795 DWORD eFlags = ( a_eClearFlags & 1 ) ? D3DCLEAR_TARGET : 0;
796
797 if ( a_eClearFlags & 2 )
798 {
799 eFlags = eFlags | D3DCLEAR_ZBUFFER;
800 TMath::Clip( a_fZ, 0.0f, 1.0f );
801 }
802
803 if ( a_eClearFlags & 4 )
804 {
805 eFlags = eFlags | D3DCLEAR_STENCIL;
806 }
807
808 // Clear the specified region
809 m_pDirectDevice->Clear(
810 0,
811 NULL,
812 eFlags,
813 ( ( a_uiColorR | 0xffffff00 ) << 8 | (TUINT)a_uiColorG ) << 8 | (TUINT)a_uiColorB,
814 a_fZ,
815 a_uiStencil
816 );
817}
818
819// $Barnyard: FUNCTION 006c6760
821{
822 switch ( a_eTextureFormat )
823 {
824 case 1:
825 return IsTextureFormatSupportedImpl( D3DFMT_A8R8G8B8 );
826 case 2:
827 return IsTextureFormatSupportedImpl( D3DFMT_X8R8G8B8 );
828 case 3:
829 return IsTextureFormatSupportedImpl( D3DFMT_A1R5G5B5 );
830 case 4:
831 return TTRUE;
832 default:
833 return TFALSE;
834 }
835}
836
837// $Barnyard: FUNCTION 006c6380
839{
840 auto pDevice = GetCurrentDevice();
841
842 return SUCCEEDED(
843 m_pDirect3D->CheckDeviceFormat(
844 pDevice->GetMode()->GetAdapter()->GetAdapterIndex(),
845 TD3DAdapter::Mode::Device::DEVICETYPES[ pDevice->GetDeviceIndex() ],
846 m_PresentParams.BackBufferFormat,
847 0,
848 D3DRTYPE_TEXTURE,
849 a_eFormat
850 )
851 );
852}
853
854// $Barnyard: FUNCTION 006c63f0
859
860// $Barnyard: FUNCTION 006c5800
862{
863 CHAR caption[ 1024 ];
864 CHAR text[ 1024 ];
865
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 );
869}
870
871// $Barnyard: FUNCTION 006c5870
873{
874 CHAR caption[ 1024 ];
875 CHAR text[ 1024 ];
876
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 );
880}
881
882// $Barnyard: FUNCTION 006c72a0
883// Creates the DirectX 8 render interface
885{
886 TASSERT( TFALSE == IsCreated() );
887
889 {
890 // Create Direct3D 8 interface
891 m_pDirect3D = Direct3DCreate8( D3D_SDK_VERSION );
892
893 if ( m_pDirect3D )
894 {
895 // Initialize adapter database and create accelerator table
898
899 // Create window
900 if ( m_Window.Create( this, a_szWindowName ) )
901 {
902 // Create system resources if needed
904 {
906 }
907
908 return TTRUE;
909 }
910 }
911 else
912 {
914 }
915 }
916
917 return TFALSE;
918}
919
920// $Barnyard: FUNCTION 006c7090
922{
923 UINT uiAdapterCount = m_pDirect3D->GetAdapterCount();
924
925 for ( UINT i = 0; i < uiAdapterCount; i++ )
926 {
927 auto pAdapter = new TD3DAdapter();
928 pAdapter->SetAdapterIndex( i );
929
930 D3DDISPLAYMODE displayMode;
931 auto pIdentifier = pAdapter->GetD3DIdentifier8();
932 m_pDirect3D->GetAdapterIdentifier( i, D3DENUM_NO_WHQL_LEVEL, pIdentifier );
933 m_pDirect3D->GetAdapterDisplayMode( i, &displayMode );
934
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 );
944
945 pAdapter->GetMode().SetD3DDisplayMode( displayMode );
946 pAdapter->EnumerateOutputs( this );
947
948 GetAdapterList()->InsertTail( pAdapter );
949 }
950}
951
953{
954 if ( m_AcceleratorTable )
955 {
956 DestroyAcceleratorTable( m_AcceleratorTable );
957 m_AcceleratorTable = NULL;
958 }
959}
960
961// $Barnyard: FUNCTION 006c6640
963{
965
966 ACCEL accel[ 2 ];
967 accel[ 0 ].fVirt = 1;
968 accel[ 0 ].key = 27;
969 accel[ 0 ].cmd = 0;
970 accel[ 1 ].fVirt = 16;
971 accel[ 1 ].key = 13;
972 accel[ 1 ].cmd = 0;
973 m_AcceleratorTable = CreateAcceleratorTableA( accel, 2 );
974}
975
976// $Barnyard: FUNCTION 006ded40
978{
979 return DXGetErrorString8A( a_eError );
980}
981
982// $Barnyard: FUNCTION 006e16f0
984{
985 return DXGetErrorDescription8A( a_eError );
986}
987
989{
990 m_OrderTables.Insert( a_pOrderTable );
991}
992
@ SYSRESOURCE_TEXTUREFACTORY
Definition TRender.h:20
ADDRESSINGMODE
Texture addressing modes.
Definition TRender.h:39
@ ADDRESSINGMODE_CLAMP
Definition TRender.h:42
@ ADDRESSINGMODE_WRAP
Definition TRender.h:40
@ ADDRESSINGMODE_MIRROR
Definition TRender.h:41
@ ADDRESSINGMODE_BORDER
Definition TRender.h:43
TEXCOORD
Texture coordinate types for addressing modes.
Definition TRender.h:28
@ TEXCOORD_UV
Definition TRender.h:29
@ TEXCOORD_U
Definition TRender.h:30
@ TEXCOORD_V
Definition TRender.h:31
Shader system for the Toshi engine.
#define TIMPLEMENT()
Definition Defines.h:136
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TERROR(...)
Definition Defines.h:153
#define HASANYFLAG(STATE, FLAG)
Definition Defines.h:5
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TTODO(DESC)
Definition Defines.h:134
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TClassObjectName
Definition TObject.h:12
#define TDEFINE_CLASS(...)
Definition TObject.h:120
int32_t TINT32
Definition Typedefs.h:12
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
uint8_t TUINT8
Definition Typedefs.h:17
float TFLOAT
Definition Typedefs.h:4
#define TNULL
Definition Typedefs.h:23
uint32_t TUINT32
Definition Typedefs.h:13
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TFORCEINLINE void Clip(T &rVal, const T &Min, const T &Max)
Definition TTRB.h:253
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
TGenericEmitter m_BeginSceneEmitter
virtual TBOOL BeginScene()
T * GetSystemResource(SYSRESOURCE systemResource)
TTexture * m_pInvalidTexture
TNodeList< TRenderAdapter > * GetAdapterList()
virtual TBOOL Destroy()
virtual TBOOL Create()
TRenderAdapter::Mode::Device * FindDevice(const DISPLAYPARAMS &a_rDisplayParams)
virtual TBOOL CreateDisplay(const DISPLAYPARAMS &a_rParams)
virtual TBOOL CreateSystemResources()
static TShaderList sm_oShaderList
Definition TShader.h:90
void InsertTail(T *a_pNode)
Definition TNodeList.h:220
static TString8 VarArgs(const TCHAR *a_pcFormat,...)
Definition TString8.cpp:202
static void MemClear(void *ptr, TSIZE size)
Definition TUtil.h:91