Advertisement
Guest User

Direct3D device

a guest
Apr 24th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.74 KB | None | 0 0
  1.  
  2. //--------------------------------------------------------------------------------------
  3. // Entry point to the program. Initializes everything and goes into a message processing
  4. // loop. Idle time is used to render the scene.
  5. //--------------------------------------------------------------------------------------
  6. int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
  7. {
  8. // Enable run-time memory check for debug builds.
  9. #if defined(DEBUG) | defined(_DEBUG)
  10. _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  11. #endif
  12.  
  13. // DXUT will create and use the best device (either D3D9 or D3D11)
  14. // that is available on the system depending on which D3D callbacks are set below
  15.  
  16. // Set DXUT callbacks
  17. DXUTSetCallbackMsgProc( MsgProc );
  18. DXUTSetCallbackKeyboard( OnKeyboard );
  19. DXUTSetCallbackFrameMove( OnFrameMove );
  20. DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
  21.  
  22. DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
  23. DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
  24. DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
  25. DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
  26. DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
  27. DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
  28.  
  29. DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
  30. DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
  31. DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
  32. DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
  33. DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
  34. DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
  35.  
  36. InitApp();
  37. DXUTInit( true, true, NULL ); // Parse the command line, show msgboxes on error, no extra command line params
  38. DXUTSetCursorSettings( true, true );
  39. DXUTCreateWindow( L"SimpleSample11" );
  40.  
  41. // Only require 10-level hardware, change to D3D_FEATURE_LEVEL_11_0 to require 11-class hardware
  42. // Switch to D3D_FEATURE_LEVEL_9_x for 10level9 hardware
  43. DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 640, 480 );
  44.  
  45. DXUTMainLoop(); // Enter into the DXUT render loop
  46.  
  47. return DXUTGetExitCode();
  48. }
  49.  
  50.  
  51. //--------------------------------------------------------------------------------------
  52. // Initialize the app
  53. //--------------------------------------------------------------------------------------
  54. void InitApp()
  55. {
  56. g_SettingsDlg.Init( &g_DialogResourceManager );
  57. g_HUD.Init( &g_DialogResourceManager );
  58. g_SampleUI.Init( &g_DialogResourceManager );
  59.  
  60. g_HUD.SetCallback( OnGUIEvent );
  61. int iY = 30;
  62. int iYo = 26;
  63. g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 22 );
  64. g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += iYo, 170, 22, VK_F3 );
  65. g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += iYo, 170, 22, VK_F2 );
  66.  
  67. g_SampleUI.SetCallback( OnGUIEvent ); iY = 10;
  68. }
  69.  
  70.  
  71. //--------------------------------------------------------------------------------------
  72. // Render the help and statistics text. This function uses the ID3DXFont interface for
  73. // efficient text rendering.
  74. //--------------------------------------------------------------------------------------
  75. void RenderText()
  76. {
  77. g_pTxtHelper->Begin();
  78. g_pTxtHelper->SetInsertionPos( 5, 5 );
  79. g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
  80. g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
  81. g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
  82. g_pTxtHelper->End();
  83. }
  84.  
  85.  
  86. //--------------------------------------------------------------------------------------
  87. // Reject any D3D11 devices that aren't acceptable by returning false
  88. //--------------------------------------------------------------------------------------
  89. bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo,
  90. DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
  91. {
  92. return true;
  93. }
  94.  
  95.  
  96. //--------------------------------------------------------------------------------------
  97. // Create any D3D11 resources that aren't dependant on the back buffer
  98. //--------------------------------------------------------------------------------------
  99. HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
  100. void* pUserContext )
  101. {
  102. HRESULT hr;
  103.  
  104. ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
  105. V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) );
  106. V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) );
  107. g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 );
  108.  
  109. // Read the HLSL file
  110. WCHAR str[MAX_PATH];
  111. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SimpleSample.hlsl" ) );
  112.  
  113. // Compile the shaders
  114. DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
  115. #if defined( DEBUG ) || defined( _DEBUG )
  116. // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
  117. // Setting this flag improves the shader debugging experience, but still allows
  118. // the shaders to be optimized and to run exactly the way they will run in
  119. // the release configuration of this program.
  120. dwShaderFlags |= D3DCOMPILE_DEBUG;
  121. #endif
  122.  
  123. // You should use the lowest possible shader profile for your shader to enable various feature levels. These
  124. // shaders are simple enough to work well within the lowest possible profile, and will run on all feature levels
  125. ID3DBlob* pVertexShaderBuffer = NULL;
  126. V_RETURN( D3DX11CompileFromFile( str, NULL, NULL, "RenderSceneVS", "vs_4_0_level_9_1", dwShaderFlags, 0, NULL,
  127. &pVertexShaderBuffer, NULL, NULL ) );
  128.  
  129. ID3DBlob* pPixelShaderBuffer = NULL;
  130. V_RETURN( D3DX11CompileFromFile( str, NULL, NULL, "RenderScenePS", "ps_4_0_level_9_1", dwShaderFlags, 0, NULL,
  131. &pPixelShaderBuffer, NULL, NULL ) );
  132.  
  133. // Create the shaders
  134. V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(),
  135. pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader11 ) );
  136. DXUT_SetDebugName( g_pVertexShader11, "RenderSceneVS" );
  137.  
  138. V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(),
  139. pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader11 ) );
  140. DXUT_SetDebugName( g_pPixelShader11, "RenderScenePS" );
  141.  
  142. // Create a layout for the object data
  143. const D3D11_INPUT_ELEMENT_DESC layout[] =
  144. {
  145. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  146. { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  147. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  148. };
  149.  
  150. V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(),
  151. pVertexShaderBuffer->GetBufferSize(), &g_pLayout11 ) );
  152. DXUT_SetDebugName( g_pLayout11, "Primary" );
  153.  
  154. // No longer need the shader blobs
  155. SAFE_RELEASE( pVertexShaderBuffer );
  156. SAFE_RELEASE( pPixelShaderBuffer );
  157.  
  158. // Create state objects
  159. D3D11_SAMPLER_DESC samDesc;
  160. ZeroMemory( &samDesc, sizeof(samDesc) );
  161. samDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  162. samDesc.AddressU = samDesc.AddressV = samDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  163. samDesc.MaxAnisotropy = 1;
  164. samDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  165. samDesc.MaxLOD = D3D11_FLOAT32_MAX;
  166. V_RETURN( pd3dDevice->CreateSamplerState( &samDesc, &g_pSamLinear ) );
  167. DXUT_SetDebugName( g_pSamLinear, "Linear" );
  168.  
  169. // Create constant buffers
  170. D3D11_BUFFER_DESC cbDesc;
  171. ZeroMemory( &cbDesc, sizeof(cbDesc) );
  172. cbDesc.Usage = D3D11_USAGE_DYNAMIC;
  173. cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  174. cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  175.  
  176. cbDesc.ByteWidth = sizeof( CB_VS_PER_OBJECT );
  177. V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, NULL, &g_pcbVSPerObject11 ) );
  178. DXUT_SetDebugName( g_pcbVSPerObject11, "CB_VS_PER_OBJECT" );
  179.  
  180. cbDesc.ByteWidth = sizeof( CB_VS_PER_FRAME );
  181. V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, NULL, &g_pcbVSPerFrame11 ) );
  182. DXUT_SetDebugName( g_pcbVSPerFrame11, "CB_VS_PER_FRAME" );
  183.  
  184. // Create other render resources here
  185.  
  186. // Setup the camera's view parameters
  187. D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f );
  188. D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f );
  189. g_Camera.SetViewParams( &vecEye, &vecAt );
  190.  
  191. return S_OK;
  192. }
  193.  
  194.  
  195. //--------------------------------------------------------------------------------------
  196. // Create any D3D11 resources that depend on the back buffer
  197. //--------------------------------------------------------------------------------------
  198. HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
  199. const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
  200. {
  201. HRESULT hr;
  202.  
  203. V_RETURN( g_DialogResourceManager.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
  204. V_RETURN( g_SettingsDlg.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
  205.  
  206. // Setup the camera's projection parameters
  207. float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
  208. g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f );
  209. g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
  210. g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON );
  211.  
  212. g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 );
  213. g_HUD.SetSize( 170, 170 );
  214. g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 300 );
  215. g_SampleUI.SetSize( 170, 300 );
  216.  
  217. return S_OK;
  218. }
  219.  
  220.  
  221. //--------------------------------------------------------------------------------------
  222. // Render the scene using the D3D11 device
  223. //--------------------------------------------------------------------------------------
  224. void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
  225. float fElapsedTime, void* pUserContext )
  226. {
  227. // If the settings dialog is being shown, then render it instead of rendering the app's scene
  228. if( g_SettingsDlg.IsActive() )
  229. {
  230. g_SettingsDlg.OnRender( fElapsedTime );
  231. return;
  232. }
  233.  
  234. float ClearColor[4] = { 0.176f, 0.196f, 0.667f, 0.0f };
  235. ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
  236. pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor );
  237.  
  238. // Clear the depth stencil
  239. ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
  240. pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );
  241.  
  242. // Get the projection & view matrix from the camera class
  243. D3DXMATRIX mWorld = *g_Camera.GetWorldMatrix();
  244. D3DXMATRIX mView = *g_Camera.GetViewMatrix();
  245. D3DXMATRIX mProj = *g_Camera.GetProjMatrix();
  246. D3DXMATRIX mWorldViewProjection = mWorld * mView * mProj;
  247.  
  248. // Set the constant buffers
  249. HRESULT hr;
  250. D3D11_MAPPED_SUBRESOURCE MappedResource;
  251. V( pd3dImmediateContext->Map( g_pcbVSPerFrame11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
  252. CB_VS_PER_FRAME* pVSPerFrame = ( CB_VS_PER_FRAME* )MappedResource.pData;
  253. pVSPerFrame->m_vLightDir = D3DXVECTOR3( 0,0.707f,-0.707f );
  254. pVSPerFrame->m_fTime = (float)fTime;
  255. pVSPerFrame->m_LightDiffuse = D3DXVECTOR4( 1.f, 1.f, 1.f, 1.f );
  256. pd3dImmediateContext->Unmap( g_pcbVSPerFrame11, 0 );
  257. pd3dImmediateContext->VSSetConstantBuffers( 1, 1, &g_pcbVSPerFrame11 );
  258.  
  259. V( pd3dImmediateContext->Map( g_pcbVSPerObject11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
  260. CB_VS_PER_OBJECT* pVSPerObject = ( CB_VS_PER_OBJECT* )MappedResource.pData;
  261. D3DXMatrixTranspose( &pVSPerObject->m_mWorldViewProjection, &mWorldViewProjection );
  262. D3DXMatrixTranspose( &pVSPerObject->m_mWorld, &mWorld );
  263. pVSPerObject->m_MaterialAmbientColor = D3DXVECTOR4( 0.3f, 0.3f, 0.3f, 1.0f );
  264. pVSPerObject->m_MaterialDiffuseColor = D3DXVECTOR4( 0.7f, 0.7f, 0.7f, 1.0f );
  265. pd3dImmediateContext->Unmap( g_pcbVSPerObject11, 0 );
  266. pd3dImmediateContext->VSSetConstantBuffers( 0, 1, &g_pcbVSPerObject11 );
  267.  
  268. // Set render resources
  269. pd3dImmediateContext->IASetInputLayout( g_pLayout11 );
  270. pd3dImmediateContext->VSSetShader( g_pVertexShader11, NULL, 0 );
  271. pd3dImmediateContext->PSSetShader( g_pPixelShader11, NULL, 0 );
  272. pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );
  273.  
  274. // Render objects here...
  275.  
  276. DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
  277. g_HUD.OnRender( fElapsedTime );
  278. g_SampleUI.OnRender( fElapsedTime );
  279. RenderText();
  280. DXUT_EndPerfEvent();
  281.  
  282. static DWORD dwTimefirst = GetTickCount();
  283. if ( GetTickCount() - dwTimefirst > 5000 )
  284. {
  285. OutputDebugString( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
  286. OutputDebugString( L"\n" );
  287. dwTimefirst = GetTickCount();
  288. }
  289. }
  290.  
  291.  
  292. //--------------------------------------------------------------------------------------
  293. // Release D3D11 resources created in OnD3D11ResizedSwapChain
  294. //--------------------------------------------------------------------------------------
  295. void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext )
  296. {
  297. g_DialogResourceManager.OnD3D11ReleasingSwapChain();
  298. }
  299.  
  300.  
  301. //--------------------------------------------------------------------------------------
  302. // Release D3D11 resources created in OnD3D11CreateDevice
  303. //--------------------------------------------------------------------------------------
  304. void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
  305. {
  306. g_DialogResourceManager.OnD3D11DestroyDevice();
  307. g_SettingsDlg.OnD3D11DestroyDevice();
  308. DXUTGetGlobalResourceCache().OnDestroyDevice();
  309. SAFE_DELETE( g_pTxtHelper );
  310.  
  311. SAFE_RELEASE( g_pVertexShader11 );
  312. SAFE_RELEASE( g_pPixelShader11 );
  313. SAFE_RELEASE( g_pLayout11 );
  314. SAFE_RELEASE( g_pSamLinear );
  315.  
  316. // Delete additional render resources here...
  317.  
  318. SAFE_RELEASE( g_pcbVSPerObject11 );
  319. SAFE_RELEASE( g_pcbVSPerFrame11 );
  320. }
  321.  
  322.  
  323. //--------------------------------------------------------------------------------------
  324. // Called right before creating a D3D9 or D3D11 device, allowing the app to modify the device settings as needed
  325. //--------------------------------------------------------------------------------------
  326. bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
  327. {
  328. if( pDeviceSettings->ver == DXUT_D3D9_DEVICE )
  329. {
  330. IDirect3D9* pD3D = DXUTGetD3D9Object();
  331. D3DCAPS9 Caps;
  332. pD3D->GetDeviceCaps( pDeviceSettings->d3d9.AdapterOrdinal, pDeviceSettings->d3d9.DeviceType, &Caps );
  333.  
  334. // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW
  335. // then switch to SWVP.
  336. if( ( Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) == 0 ||
  337. Caps.VertexShaderVersion < D3DVS_VERSION( 1, 1 ) )
  338. {
  339. pDeviceSettings->d3d9.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  340. }
  341.  
  342. // Debugging vertex shaders requires either REF or software vertex processing
  343. // and debugging pixel shaders requires REF.
  344. #ifdef DEBUG_VS
  345. if( pDeviceSettings->d3d9.DeviceType != D3DDEVTYPE_REF )
  346. {
  347. pDeviceSettings->d3d9.BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
  348. pDeviceSettings->d3d9.BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
  349. pDeviceSettings->d3d9.BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  350. }
  351. #endif
  352. #ifdef DEBUG_PS
  353. pDeviceSettings->d3d9.DeviceType = D3DDEVTYPE_REF;
  354. #endif
  355. }
  356.  
  357. // For the first device created if its a REF device, optionally display a warning dialog box
  358. static bool s_bFirstTime = true;
  359. if( s_bFirstTime )
  360. {
  361. s_bFirstTime = false;
  362. if( ( DXUT_D3D9_DEVICE == pDeviceSettings->ver && pDeviceSettings->d3d9.DeviceType == D3DDEVTYPE_REF ) ||
  363. ( DXUT_D3D11_DEVICE == pDeviceSettings->ver &&
  364. pDeviceSettings->d3d11.DriverType == D3D_DRIVER_TYPE_REFERENCE ) )
  365. {
  366. DXUTDisplaySwitchingToREFWarning( pDeviceSettings->ver );
  367. }
  368.  
  369. }
  370.  
  371. return true;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement