|
| 1 | +//-------------------------------------------------------------------------------------- |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// Licensed under the MIT License. |
| 4 | +// |
| 5 | +// File: BackgroundThread.cpp |
| 6 | +// |
| 7 | +// Sample showing how to use D3D9Ex advanced features. |
| 8 | +// |
| 9 | +//-------------------------------------------------------------------------------------- |
| 10 | +#include <windows.h> |
| 11 | +#include <d3d9.h> |
| 12 | +#include <d3dx9.h> |
| 13 | + |
| 14 | +#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p) = NULL; } } |
| 15 | + |
| 16 | +//----------------------------------------------------------------------------- |
| 17 | +// structures |
| 18 | +//----------------------------------------------------------------------------- |
| 19 | +struct BACKGROUNDVERTEX |
| 20 | +{ |
| 21 | + FLOAT x, y, z; // The untransformed position for the vertex |
| 22 | + DWORD dwColor; // Color |
| 23 | +}; |
| 24 | +#define D3DFVF_BACKGROUNDVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE) |
| 25 | + |
| 26 | +//-------------------------------------------------------------------------------------- |
| 27 | +// Global variables |
| 28 | +//-------------------------------------------------------------------------------------- |
| 29 | +bool g_bEndThread = false; |
| 30 | +IDirect3DTexture9* g_pRenderTarget = NULL; |
| 31 | +IDirect3DTexture9* g_pSharedTexture = NULL; |
| 32 | +IDirect3DVertexBuffer9* g_pCubeVB = NULL; |
| 33 | +IDirect3DIndexBuffer9* g_pCubeIB = NULL; |
| 34 | +HANDLE g_SharedHandle = NULL; |
| 35 | +UINT g_RTWidth = 1024; |
| 36 | +UINT g_RTHeight = 1024; |
| 37 | +int g_CubeCubes = 3; |
| 38 | +float g_BoxRad = 30.0f; |
| 39 | +CRITICAL_SECTION g_CSCubes; |
| 40 | + |
| 41 | +float g_fBackLastFrameTime = 0.0f; |
| 42 | +LARGE_INTEGER g_liBackLastTimerUpdate = {0}; |
| 43 | +LARGE_INTEGER g_liBackTimerFrequency = {0}; |
| 44 | + |
| 45 | +//-------------------------------------------------------------------------------------- |
| 46 | +// Function Prototypes and Externs |
| 47 | +//-------------------------------------------------------------------------------------- |
| 48 | +extern HRESULT CreateD3D9VDevice( IDirect3D9Ex* pD3D, IDirect3DDevice9Ex** ppDev9Ex, D3DPRESENT_PARAMETERS* pD3DPresentParameters, HWND hWnd ); |
| 49 | +DWORD WINAPI BackgroundThreadProc( LPVOID lpParam ); |
| 50 | +HRESULT CreateCube(IDirect3DDevice9Ex* pDev); |
| 51 | +HRESULT CreateSharedRenderTexture(IDirect3DDevice9Ex* pDev); |
| 52 | +void RenderBackground(IDirect3DDevice9Ex* pDev); |
| 53 | +void CleanupBackground(); |
| 54 | + |
| 55 | + |
| 56 | +//-------------------------------------------------------------------------------------- |
| 57 | +// Exported |
| 58 | +//-------------------------------------------------------------------------------------- |
| 59 | +HANDLE CreateBackgroundThread(IDirect3D9Ex* pD3D9); |
| 60 | +void KillBackgroundThread() { g_bEndThread = true; } |
| 61 | +HANDLE GetSharedTextureHandle() { return g_SharedHandle; } |
| 62 | +int IncreaseCubeCount(); |
| 63 | +int DecreaseCubeCount(); |
| 64 | +float GetFPS(); |
| 65 | + |
| 66 | + |
| 67 | +// main background thread proc |
| 68 | +DWORD WINAPI BackgroundThreadProc( LPVOID lpParam ) |
| 69 | +{ |
| 70 | + // Create a critsec |
| 71 | + InitializeCriticalSection( &g_CSCubes ); |
| 72 | + |
| 73 | + // Create a d3d9V device |
| 74 | + IDirect3DDevice9Ex* pDevBackground = NULL; |
| 75 | + IDirect3D9Ex* pD3D9 = (IDirect3D9Ex*)(lpParam); |
| 76 | + |
| 77 | + D3DPRESENT_PARAMETERS d3dpp = {0}; |
| 78 | + |
| 79 | + d3dpp.Windowed = TRUE; |
| 80 | + d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 81 | + d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; |
| 82 | + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; |
| 83 | + |
| 84 | + if( FAILED( CreateD3D9VDevice( pD3D9, &pDevBackground, &d3dpp, NULL ) ) ) |
| 85 | + return 1; |
| 86 | + if( FAILED( CreateCube( pDevBackground ) ) ) |
| 87 | + return 2; |
| 88 | + if( FAILED( CreateSharedRenderTexture( pDevBackground ) ) ) |
| 89 | + return 3; |
| 90 | + |
| 91 | + // Set the GPU thread priority |
| 92 | + pDevBackground->SetGPUThreadPriority( -7 ); |
| 93 | + SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_LOWEST ); |
| 94 | + |
| 95 | + // Get Timer Frequency |
| 96 | + QueryPerformanceFrequency( &g_liBackTimerFrequency ); |
| 97 | + QueryPerformanceCounter( &g_liBackLastTimerUpdate ); |
| 98 | + |
| 99 | + while(!g_bEndThread) |
| 100 | + { |
| 101 | + RenderBackground( pDevBackground ); |
| 102 | + // Uncomment next line to throttle rendering to no more then the refresh rate of the monitor. |
| 103 | + // pDevBackground->WaitForVBlank(0); |
| 104 | + } |
| 105 | + |
| 106 | + // Cleanup |
| 107 | + DeleteCriticalSection( &g_CSCubes ); |
| 108 | + CleanupBackground(); |
| 109 | + SAFE_RELEASE( pDevBackground ); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +HANDLE CreateBackgroundThread(IDirect3D9Ex* pD3D9) |
| 115 | +{ |
| 116 | + // create the thread |
| 117 | + DWORD dwThreadID = 0; |
| 118 | + HANDLE hBackgroundThread = NULL; |
| 119 | + hBackgroundThread = CreateThread( NULL, |
| 120 | + 0, |
| 121 | + BackgroundThreadProc, |
| 122 | + (LPVOID)pD3D9, |
| 123 | + CREATE_SUSPENDED, |
| 124 | + &dwThreadID ); |
| 125 | + |
| 126 | + if( hBackgroundThread ) |
| 127 | + { |
| 128 | + // set the priority |
| 129 | + |
| 130 | + // resume the thread |
| 131 | + ResumeThread( hBackgroundThread ); |
| 132 | + } |
| 133 | + |
| 134 | + return hBackgroundThread; |
| 135 | +} |
| 136 | + |
| 137 | +HRESULT CreateCube( IDirect3DDevice9Ex* pDev ) |
| 138 | +{ |
| 139 | + // create the vb |
| 140 | + if( FAILED( pDev->CreateVertexBuffer( 8*sizeof(BACKGROUNDVERTEX), |
| 141 | + 0, |
| 142 | + D3DFVF_BACKGROUNDVERTEX, |
| 143 | + D3DPOOL_DEFAULT, |
| 144 | + &g_pCubeVB, |
| 145 | + NULL ) ) ) |
| 146 | + { |
| 147 | + return E_FAIL; |
| 148 | + } |
| 149 | + |
| 150 | + BACKGROUNDVERTEX vertices[] = |
| 151 | + { |
| 152 | + { -1.0f, 1.0f, -1.0f, 0x000066 }, |
| 153 | + { 1.0f, 1.0f, -1.0f, 0x006600 }, |
| 154 | + { 1.0f, 1.0f, 1.0f, 0x006666 }, |
| 155 | + { -1.0f, 1.0f, 1.0f, 0x660000 }, |
| 156 | + { -1.0f, -1.0f, -1.0f, 0x660066 }, |
| 157 | + { 1.0f, -1.0f, -1.0f, 0x666600 }, |
| 158 | + { 1.0f, -1.0f, 1.0f, 0x666666 }, |
| 159 | + { -1.0f, -1.0f, 1.0f, 0x000000 }, |
| 160 | + }; |
| 161 | + |
| 162 | + VOID* pVertices = NULL; |
| 163 | + if( FAILED( g_pCubeVB->Lock( 0, sizeof(vertices), &pVertices, 0 ) ) ) |
| 164 | + return E_FAIL; |
| 165 | + memcpy( pVertices, vertices, sizeof(vertices) ); |
| 166 | + g_pCubeVB->Unlock(); |
| 167 | + |
| 168 | + |
| 169 | + if( FAILED( pDev->CreateIndexBuffer( 36*sizeof(WORD), |
| 170 | + 0, |
| 171 | + D3DFMT_INDEX16, |
| 172 | + D3DPOOL_DEFAULT, |
| 173 | + &g_pCubeIB, |
| 174 | + NULL ) ) ) |
| 175 | + { |
| 176 | + return E_FAIL; |
| 177 | + } |
| 178 | + |
| 179 | + WORD indices[] = |
| 180 | + { |
| 181 | + 3,1,0, |
| 182 | + 2,1,3, |
| 183 | + |
| 184 | + 0,5,4, |
| 185 | + 1,5,0, |
| 186 | + |
| 187 | + 3,4,7, |
| 188 | + 0,4,3, |
| 189 | + |
| 190 | + 1,6,5, |
| 191 | + 2,6,1, |
| 192 | + |
| 193 | + 2,7,6, |
| 194 | + 3,7,2, |
| 195 | + |
| 196 | + 6,4,5, |
| 197 | + 7,4,6, |
| 198 | + }; |
| 199 | + |
| 200 | + VOID* pIndices = NULL; |
| 201 | + if( FAILED( g_pCubeIB->Lock( 0, sizeof(indices), &pIndices, 0 ) ) ) |
| 202 | + return E_FAIL; |
| 203 | + memcpy( pIndices, indices, sizeof(indices) ); |
| 204 | + g_pCubeIB->Unlock(); |
| 205 | + |
| 206 | + return S_OK; |
| 207 | +} |
| 208 | + |
| 209 | +HRESULT CreateSharedRenderTexture(IDirect3DDevice9Ex* pDev) |
| 210 | +{ |
| 211 | + if( FAILED( pDev->CreateTexture( g_RTWidth, |
| 212 | + g_RTHeight, |
| 213 | + 1, |
| 214 | + D3DUSAGE_RENDERTARGET, |
| 215 | + D3DFMT_X8R8G8B8, |
| 216 | + D3DPOOL_DEFAULT, |
| 217 | + &g_pSharedTexture, |
| 218 | + &g_SharedHandle ) ) ) |
| 219 | + { |
| 220 | + return E_FAIL; |
| 221 | + } |
| 222 | + |
| 223 | + if( !g_SharedHandle ) |
| 224 | + return E_FAIL; |
| 225 | + |
| 226 | + // create a render target |
| 227 | + if( FAILED( pDev->CreateTexture( g_RTWidth, |
| 228 | + g_RTHeight, |
| 229 | + 1, |
| 230 | + D3DUSAGE_RENDERTARGET, |
| 231 | + D3DFMT_X8R8G8B8, |
| 232 | + D3DPOOL_DEFAULT, |
| 233 | + &g_pRenderTarget, |
| 234 | + NULL ) ) ) |
| 235 | + { |
| 236 | + return E_FAIL; |
| 237 | + } |
| 238 | + |
| 239 | + IDirect3DSurface9* pRTSurf = NULL; |
| 240 | + if( FAILED(g_pRenderTarget->GetSurfaceLevel( 0, &pRTSurf ) ) ) |
| 241 | + return E_FAIL; |
| 242 | + |
| 243 | + if( FAILED(pDev->SetRenderTarget( 0, pRTSurf )) ) |
| 244 | + return E_FAIL; |
| 245 | + |
| 246 | + pRTSurf->Release(); |
| 247 | + |
| 248 | + // viewport |
| 249 | + D3DVIEWPORT9 vp; |
| 250 | + vp.X = 0; |
| 251 | + vp.Y = 0; |
| 252 | + vp.Width = g_RTWidth; |
| 253 | + vp.Height = g_RTHeight; |
| 254 | + vp.MinZ = 0.0f; |
| 255 | + vp.MaxZ = 1.0f; |
| 256 | + pDev->SetViewport( &vp ); |
| 257 | + |
| 258 | + return S_OK; |
| 259 | +} |
| 260 | + |
| 261 | +void RenderBackground( IDirect3DDevice9Ex* pDev ) |
| 262 | +{ |
| 263 | + static float fRot = 0.0f; |
| 264 | + fRot += g_fBackLastFrameTime*60.0f*(D3DX_PI/180.0f); |
| 265 | + |
| 266 | + // setup the matrices |
| 267 | + D3DXMATRIX mWorld; |
| 268 | + D3DXMATRIX mView; |
| 269 | + D3DXMATRIX mProj; |
| 270 | + D3DXMatrixRotationY( &mWorld, fRot ); |
| 271 | + |
| 272 | + D3DXVECTOR3 eye( 0, 2.0f, g_BoxRad*3.5f ); |
| 273 | + D3DXVECTOR3 at( 0, 0, 0 ); |
| 274 | + D3DXVECTOR3 up( 0, 1, 0 ); |
| 275 | + D3DXMatrixLookAtLH( &mView, &eye, &at, &up ); |
| 276 | + D3DXMatrixPerspectiveFovLH( &mProj, D3DX_PI/4.0f, 1.0f, 0.1f, 1000.0f ); |
| 277 | + |
| 278 | + pDev->SetTransform( D3DTS_VIEW, &mView ); |
| 279 | + pDev->SetTransform( D3DTS_PROJECTION, &mProj ); |
| 280 | + |
| 281 | + // clear |
| 282 | + HRESULT hr = S_OK; |
| 283 | + pDev->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); |
| 284 | + |
| 285 | + // Begin the scene |
| 286 | + if( SUCCEEDED( pDev->BeginScene() ) ) |
| 287 | + { |
| 288 | + // set the texture stage states |
| 289 | + pDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); |
| 290 | + pDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ); |
| 291 | + |
| 292 | + // disable lighting |
| 293 | + pDev->SetRenderState( D3DRS_LIGHTING, FALSE ); |
| 294 | + |
| 295 | + // stream sources |
| 296 | + pDev->SetStreamSource( 0, g_pCubeVB, 0, sizeof(BACKGROUNDVERTEX) ); |
| 297 | + pDev->SetIndices( g_pCubeIB ); |
| 298 | + pDev->SetFVF( D3DFVF_BACKGROUNDVERTEX ); |
| 299 | + |
| 300 | + // draw |
| 301 | + float fStep = (g_BoxRad*2) / g_CubeCubes; |
| 302 | + float fStart = -g_BoxRad + fStep/2.0f; |
| 303 | + float fStop = fStart + fStep*g_CubeCubes; |
| 304 | + |
| 305 | + EnterCriticalSection( &g_CSCubes ); |
| 306 | + for( float z=fStart; z<fStop; z+=fStep ) |
| 307 | + { |
| 308 | + for( float y=fStart; y<fStop; y+=fStep ) |
| 309 | + { |
| 310 | + for( float x=fStart; x<fStop; x+=fStep ) |
| 311 | + { |
| 312 | + D3DXMATRIX mPos; |
| 313 | + D3DXMatrixTranslation( &mPos, x,y,z ); |
| 314 | + mPos = mWorld*mPos; |
| 315 | + pDev->SetTransform( D3DTS_WORLD, &mPos ); |
| 316 | + |
| 317 | + hr = pDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 ); |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + LeaveCriticalSection( &g_CSCubes ); |
| 322 | + |
| 323 | + // End the scene |
| 324 | + pDev->EndScene(); |
| 325 | + } |
| 326 | + |
| 327 | + // Stretch rect to our shared texture |
| 328 | + IDirect3DSurface9* pSurfSrc; |
| 329 | + IDirect3DSurface9* pSurfDest; |
| 330 | + g_pRenderTarget->GetSurfaceLevel( 0, &pSurfSrc ); |
| 331 | + g_pSharedTexture->GetSurfaceLevel( 0, &pSurfDest ); |
| 332 | + hr = pDev->StretchRect( pSurfSrc, NULL, pSurfDest, NULL, D3DTEXF_POINT ); |
| 333 | + SAFE_RELEASE( pSurfSrc ); |
| 334 | + SAFE_RELEASE( pSurfDest ); |
| 335 | + |
| 336 | + // Get the time |
| 337 | + LARGE_INTEGER liCurrentTime; |
| 338 | + QueryPerformanceCounter( &liCurrentTime ); |
| 339 | + g_fBackLastFrameTime = (float)(liCurrentTime.QuadPart - g_liBackLastTimerUpdate.QuadPart) / (float)g_liBackTimerFrequency.QuadPart; |
| 340 | + g_liBackLastTimerUpdate.QuadPart = liCurrentTime.QuadPart; |
| 341 | + |
| 342 | +} |
| 343 | + |
| 344 | +void CleanupBackground() |
| 345 | +{ |
| 346 | + SAFE_RELEASE(g_pRenderTarget); |
| 347 | + SAFE_RELEASE(g_pSharedTexture); |
| 348 | + SAFE_RELEASE(g_pCubeVB); |
| 349 | + SAFE_RELEASE(g_pCubeIB); |
| 350 | +} |
| 351 | + |
| 352 | +int IncreaseCubeCount() |
| 353 | +{ |
| 354 | + EnterCriticalSection( &g_CSCubes ); |
| 355 | + |
| 356 | + g_CubeCubes ++; |
| 357 | + if( g_CubeCubes > 100 ) |
| 358 | + g_CubeCubes = 100; |
| 359 | + |
| 360 | + LeaveCriticalSection( &g_CSCubes ); |
| 361 | + |
| 362 | + return g_CubeCubes; |
| 363 | +} |
| 364 | + |
| 365 | +int DecreaseCubeCount() |
| 366 | +{ |
| 367 | + EnterCriticalSection( &g_CSCubes ); |
| 368 | + |
| 369 | + g_CubeCubes --; |
| 370 | + if( g_CubeCubes < 0 ) |
| 371 | + g_CubeCubes = 0; |
| 372 | + |
| 373 | + LeaveCriticalSection( &g_CSCubes ); |
| 374 | + |
| 375 | + return g_CubeCubes; |
| 376 | +} |
| 377 | + |
| 378 | +float GetFPS() |
| 379 | +{ |
| 380 | + return 1.0f / g_fBackLastFrameTime; |
| 381 | +} |
0 commit comments