Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------------------------
- // Entry point to the program. Initializes everything and goes into a message processing
- // loop. Idle time is used to render the scene.
- //--------------------------------------------------------------------------------------
- int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
- {
- // Enable run-time memory check for debug builds.
- #if defined(DEBUG) | defined(_DEBUG)
- _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
- #endif
- // DXUT will create and use the best device (either D3D9 or D3D11)
- // that is available on the system depending on which D3D callbacks are set below
- // Set DXUT callbacks
- DXUTSetCallbackMsgProc( MsgProc );
- DXUTSetCallbackKeyboard( OnKeyboard );
- DXUTSetCallbackFrameMove( OnFrameMove );
- DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
- DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
- DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
- DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
- DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
- DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
- DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
- DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
- DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
- DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
- DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
- DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
- DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
- InitApp();
- DXUTInit( true, true, NULL ); // Parse the command line, show msgboxes on error, no extra command line params
- DXUTSetCursorSettings( true, true );
- DXUTCreateWindow( L"SimpleSample11" );
- // Only require 10-level hardware, change to D3D_FEATURE_LEVEL_11_0 to require 11-class hardware
- // Switch to D3D_FEATURE_LEVEL_9_x for 10level9 hardware
- DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 640, 480 );
- DXUTMainLoop(); // Enter into the DXUT render loop
- return DXUTGetExitCode();
- }
- //--------------------------------------------------------------------------------------
- // Initialize the app
- //--------------------------------------------------------------------------------------
- void InitApp()
- {
- g_SettingsDlg.Init( &g_DialogResourceManager );
- g_HUD.Init( &g_DialogResourceManager );
- g_SampleUI.Init( &g_DialogResourceManager );
- g_HUD.SetCallback( OnGUIEvent );
- int iY = 30;
- int iYo = 26;
- g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 22 );
- g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += iYo, 170, 22, VK_F3 );
- g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += iYo, 170, 22, VK_F2 );
- g_SampleUI.SetCallback( OnGUIEvent ); iY = 10;
- }
- //--------------------------------------------------------------------------------------
- // Render the help and statistics text. This function uses the ID3DXFont interface for
- // efficient text rendering.
- //--------------------------------------------------------------------------------------
- void RenderText()
- {
- g_pTxtHelper->Begin();
- g_pTxtHelper->SetInsertionPos( 5, 5 );
- g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
- g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
- g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
- g_pTxtHelper->End();
- }
- //--------------------------------------------------------------------------------------
- // Reject any D3D11 devices that aren't acceptable by returning false
- //--------------------------------------------------------------------------------------
- bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo,
- DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
- {
- return true;
- }
- //--------------------------------------------------------------------------------------
- // Create any D3D11 resources that aren't dependant on the back buffer
- //--------------------------------------------------------------------------------------
- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
- void* pUserContext )
- {
- HRESULT hr;
- ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
- V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) );
- V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) );
- g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 );
- // Read the HLSL file
- WCHAR str[MAX_PATH];
- V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SimpleSample.hlsl" ) );
- // Compile the shaders
- DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
- #if defined( DEBUG ) || defined( _DEBUG )
- // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
- // Setting this flag improves the shader debugging experience, but still allows
- // the shaders to be optimized and to run exactly the way they will run in
- // the release configuration of this program.
- dwShaderFlags |= D3DCOMPILE_DEBUG;
- #endif
- // You should use the lowest possible shader profile for your shader to enable various feature levels. These
- // shaders are simple enough to work well within the lowest possible profile, and will run on all feature levels
- ID3DBlob* pVertexShaderBuffer = NULL;
- V_RETURN( D3DX11CompileFromFile( str, NULL, NULL, "RenderSceneVS", "vs_4_0_level_9_1", dwShaderFlags, 0, NULL,
- &pVertexShaderBuffer, NULL, NULL ) );
- ID3DBlob* pPixelShaderBuffer = NULL;
- V_RETURN( D3DX11CompileFromFile( str, NULL, NULL, "RenderScenePS", "ps_4_0_level_9_1", dwShaderFlags, 0, NULL,
- &pPixelShaderBuffer, NULL, NULL ) );
- // Create the shaders
- V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(),
- pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader11 ) );
- DXUT_SetDebugName( g_pVertexShader11, "RenderSceneVS" );
- V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(),
- pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader11 ) );
- DXUT_SetDebugName( g_pPixelShader11, "RenderScenePS" );
- // Create a layout for the object data
- const D3D11_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- };
- V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(),
- pVertexShaderBuffer->GetBufferSize(), &g_pLayout11 ) );
- DXUT_SetDebugName( g_pLayout11, "Primary" );
- // No longer need the shader blobs
- SAFE_RELEASE( pVertexShaderBuffer );
- SAFE_RELEASE( pPixelShaderBuffer );
- // Create state objects
- D3D11_SAMPLER_DESC samDesc;
- ZeroMemory( &samDesc, sizeof(samDesc) );
- samDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- samDesc.AddressU = samDesc.AddressV = samDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- samDesc.MaxAnisotropy = 1;
- samDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
- samDesc.MaxLOD = D3D11_FLOAT32_MAX;
- V_RETURN( pd3dDevice->CreateSamplerState( &samDesc, &g_pSamLinear ) );
- DXUT_SetDebugName( g_pSamLinear, "Linear" );
- // Create constant buffers
- D3D11_BUFFER_DESC cbDesc;
- ZeroMemory( &cbDesc, sizeof(cbDesc) );
- cbDesc.Usage = D3D11_USAGE_DYNAMIC;
- cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- cbDesc.ByteWidth = sizeof( CB_VS_PER_OBJECT );
- V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, NULL, &g_pcbVSPerObject11 ) );
- DXUT_SetDebugName( g_pcbVSPerObject11, "CB_VS_PER_OBJECT" );
- cbDesc.ByteWidth = sizeof( CB_VS_PER_FRAME );
- V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, NULL, &g_pcbVSPerFrame11 ) );
- DXUT_SetDebugName( g_pcbVSPerFrame11, "CB_VS_PER_FRAME" );
- // Create other render resources here
- // Setup the camera's view parameters
- D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f );
- D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f );
- g_Camera.SetViewParams( &vecEye, &vecAt );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Create any D3D11 resources that depend on the back buffer
- //--------------------------------------------------------------------------------------
- HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
- const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
- {
- HRESULT hr;
- V_RETURN( g_DialogResourceManager.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
- V_RETURN( g_SettingsDlg.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
- // Setup the camera's projection parameters
- float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
- g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f );
- g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
- g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON );
- g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 );
- g_HUD.SetSize( 170, 170 );
- g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 300 );
- g_SampleUI.SetSize( 170, 300 );
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Render the scene using the D3D11 device
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
- float fElapsedTime, void* pUserContext )
- {
- // If the settings dialog is being shown, then render it instead of rendering the app's scene
- if( g_SettingsDlg.IsActive() )
- {
- g_SettingsDlg.OnRender( fElapsedTime );
- return;
- }
- float ClearColor[4] = { 0.176f, 0.196f, 0.667f, 0.0f };
- ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
- pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor );
- // Clear the depth stencil
- ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
- pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );
- // Get the projection & view matrix from the camera class
- D3DXMATRIX mWorld = *g_Camera.GetWorldMatrix();
- D3DXMATRIX mView = *g_Camera.GetViewMatrix();
- D3DXMATRIX mProj = *g_Camera.GetProjMatrix();
- D3DXMATRIX mWorldViewProjection = mWorld * mView * mProj;
- // Set the constant buffers
- HRESULT hr;
- D3D11_MAPPED_SUBRESOURCE MappedResource;
- V( pd3dImmediateContext->Map( g_pcbVSPerFrame11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
- CB_VS_PER_FRAME* pVSPerFrame = ( CB_VS_PER_FRAME* )MappedResource.pData;
- pVSPerFrame->m_vLightDir = D3DXVECTOR3( 0,0.707f,-0.707f );
- pVSPerFrame->m_fTime = (float)fTime;
- pVSPerFrame->m_LightDiffuse = D3DXVECTOR4( 1.f, 1.f, 1.f, 1.f );
- pd3dImmediateContext->Unmap( g_pcbVSPerFrame11, 0 );
- pd3dImmediateContext->VSSetConstantBuffers( 1, 1, &g_pcbVSPerFrame11 );
- V( pd3dImmediateContext->Map( g_pcbVSPerObject11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
- CB_VS_PER_OBJECT* pVSPerObject = ( CB_VS_PER_OBJECT* )MappedResource.pData;
- D3DXMatrixTranspose( &pVSPerObject->m_mWorldViewProjection, &mWorldViewProjection );
- D3DXMatrixTranspose( &pVSPerObject->m_mWorld, &mWorld );
- pVSPerObject->m_MaterialAmbientColor = D3DXVECTOR4( 0.3f, 0.3f, 0.3f, 1.0f );
- pVSPerObject->m_MaterialDiffuseColor = D3DXVECTOR4( 0.7f, 0.7f, 0.7f, 1.0f );
- pd3dImmediateContext->Unmap( g_pcbVSPerObject11, 0 );
- pd3dImmediateContext->VSSetConstantBuffers( 0, 1, &g_pcbVSPerObject11 );
- // Set render resources
- pd3dImmediateContext->IASetInputLayout( g_pLayout11 );
- pd3dImmediateContext->VSSetShader( g_pVertexShader11, NULL, 0 );
- pd3dImmediateContext->PSSetShader( g_pPixelShader11, NULL, 0 );
- pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );
- // Render objects here...
- DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
- g_HUD.OnRender( fElapsedTime );
- g_SampleUI.OnRender( fElapsedTime );
- RenderText();
- DXUT_EndPerfEvent();
- static DWORD dwTimefirst = GetTickCount();
- if ( GetTickCount() - dwTimefirst > 5000 )
- {
- OutputDebugString( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
- OutputDebugString( L"\n" );
- dwTimefirst = GetTickCount();
- }
- }
- //--------------------------------------------------------------------------------------
- // Release D3D11 resources created in OnD3D11ResizedSwapChain
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext )
- {
- g_DialogResourceManager.OnD3D11ReleasingSwapChain();
- }
- //--------------------------------------------------------------------------------------
- // Release D3D11 resources created in OnD3D11CreateDevice
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
- {
- g_DialogResourceManager.OnD3D11DestroyDevice();
- g_SettingsDlg.OnD3D11DestroyDevice();
- DXUTGetGlobalResourceCache().OnDestroyDevice();
- SAFE_DELETE( g_pTxtHelper );
- SAFE_RELEASE( g_pVertexShader11 );
- SAFE_RELEASE( g_pPixelShader11 );
- SAFE_RELEASE( g_pLayout11 );
- SAFE_RELEASE( g_pSamLinear );
- // Delete additional render resources here...
- SAFE_RELEASE( g_pcbVSPerObject11 );
- SAFE_RELEASE( g_pcbVSPerFrame11 );
- }
- //--------------------------------------------------------------------------------------
- // Called right before creating a D3D9 or D3D11 device, allowing the app to modify the device settings as needed
- //--------------------------------------------------------------------------------------
- bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
- {
- if( pDeviceSettings->ver == DXUT_D3D9_DEVICE )
- {
- IDirect3D9* pD3D = DXUTGetD3D9Object();
- D3DCAPS9 Caps;
- pD3D->GetDeviceCaps( pDeviceSettings->d3d9.AdapterOrdinal, pDeviceSettings->d3d9.DeviceType, &Caps );
- // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW
- // then switch to SWVP.
- if( ( Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) == 0 ||
- Caps.VertexShaderVersion < D3DVS_VERSION( 1, 1 ) )
- {
- pDeviceSettings->d3d9.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- }
- // Debugging vertex shaders requires either REF or software vertex processing
- // and debugging pixel shaders requires REF.
- #ifdef DEBUG_VS
- if( pDeviceSettings->d3d9.DeviceType != D3DDEVTYPE_REF )
- {
- pDeviceSettings->d3d9.BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
- pDeviceSettings->d3d9.BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
- pDeviceSettings->d3d9.BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- }
- #endif
- #ifdef DEBUG_PS
- pDeviceSettings->d3d9.DeviceType = D3DDEVTYPE_REF;
- #endif
- }
- // For the first device created if its a REF device, optionally display a warning dialog box
- static bool s_bFirstTime = true;
- if( s_bFirstTime )
- {
- s_bFirstTime = false;
- if( ( DXUT_D3D9_DEVICE == pDeviceSettings->ver && pDeviceSettings->d3d9.DeviceType == D3DDEVTYPE_REF ) ||
- ( DXUT_D3D11_DEVICE == pDeviceSettings->ver &&
- pDeviceSettings->d3d11.DriverType == D3D_DRIVER_TYPE_REFERENCE ) )
- {
- DXUTDisplaySwitchingToREFWarning( pDeviceSettings->ver );
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement