涓㈠け鐨勮澶?br> 涓涓狹icrosoft? Direct3D?鍙互澶勪簬鎿嶄綔鐘舵佹垨涓㈠け鐘舵併傛搷浣滅姸鎬佹槸璁懼鐨勬甯哥姸鎬侊紝璁懼鎸夐鏈熻繍琛屽茍present鎵鏈夋覆鏌撶粨鏋溿傚綋浜嬩歡鍙戠敓鏃訛紝濡傚叏灞忓簲鐢ㄧ▼搴忓け鍘婚敭鐩樿緭鍏ョ劍鐐癸紝璁懼灝辮漿鍙樺埌涓㈠け鐘舵侊紝榪欎細瀵艱嚧娓叉煋鏃犳硶榪涜銆備涪澶辯姸鎬佽〃鐜頒負鎵鏈夋覆鏌撴搷浣滅殑鎮勭劧澶辮觸錛岃繖鎰忓懗鐫鍗充嬌娓叉煋鎿嶄綔澶辮觸鎵鏈夌殑娓叉煋鏂規硶浠嶅彲浠ヨ繑鍥炴垚鍔熺爜銆傚湪榪欑鎯呭喌涓嬶紝IDirect3DDevice9: resent榪斿洖閿欒鐮丏3DERR_DEVICELOST銆?br> Direct3D鏈夋剰娌℃湁瀵瑰彲鑳藉鑷磋澶囦涪澶辯殑鎵鏈夋儏鍐佃繘琛岃緇嗚鏄庛備竴浜涘吀鍨嬬殑渚嬪瓙鍖呮嫭紿楀彛澶卞幓鐒︾偣錛屼緥濡傜敤鎴鋒寜涓嬩簡ALT+TAB鎴栧脊鍑轟簡涓涓郴緇熷璇濇銆傝澶囦篃浼氬洜涓虹數婧愮鐞嗕簨浠惰屼涪澶憋紝鎴栬呭彟涓涓簲鐢ㄧ▼搴忚繘琛屽叏灞忔搷浣溿傚彟澶栵紝浠諱綍瀵笽Direct3DDevice9::Reset璋冪敤鐨勫け璐ヤ細鎶婅澶囩疆涓轟涪澶辯姸鎬併?br> 娉ㄦ剰 鍙互淇濊瘉鎵鏈夌戶鎵胯嚜IUnknown鐨勬柟娉曞湪璁懼涓㈠け鍚庝粛鑳芥甯稿伐浣溿傝澶囦涪澶卞悗錛屾瘡涓嚱鏁頒竴鑸湁涓夌鍙兘錛?br> 璋冪敤澶辮觸錛岃繑鍥炲間負D3DERR_DEVICELOST – 榪欐剰鍛崇潃搴旂敤紼嬪簭蹇呴』鍙戠幇璁懼宸茬粡涓㈠け錛屼粠鑰岀煡閬撲竴浜涗簨鎯呮病鏈夋寜鐓ч鏈熻繘琛屻? 鎮勭劧澶辮觸錛岃繑鍥炲間負S_OK鎴栧叾瀹冨?– 鑻ュ嚱鏁拌皟鐢ㄦ倓鐒跺け璐ワ紝鍒欏簲鐢ㄧ▼搴忎竴鑸棤娉曞尯鍒嗗嚭“璋冪敤鎴愬姛”鎴?#8220;鎮勭劧澶辮觸”銆? 鍑芥暟榪斿洖涓涓繑鍥炲箋? 瀵逛涪澶辯殑璁懼浣滃嚭鍝嶅簲
璁懼鍦ㄨ閲嶇疆鍚庯紝搴旇閲嶆柊鍒涘緩璧勬簮錛堝寘鎷樉瀛樿祫婧愶級銆傚鏋滆澶囦涪澶變簡錛岄偅涔堝簲鐢ㄧ▼搴忓簲璇ユ煡璇㈣澶囩姸鎬侊紝鐪嬫槸鍚﹀彲浠ュ皢涔嬫仮澶嶅洖鎿嶄綔鐘舵併傚鏋滀笉琛岋紝閭d箞灝辯瓑鍒拌澶囧彲浠ヨ鎭㈠涓烘銆?br> 濡傛灉璁懼鍙互琚仮澶嶏紝閭d箞搴旂敤紼嬪簭搴旇閿姣佹墍鏈夋樉瀛樿祫婧愬拰浜ゆ崲閾撅紝騫跺噯澶囨仮澶嶃傜劧鍚庯紝搴旂敤紼嬪簭璋冪敤IDirect3DDevice9::Reset鏂規硶銆俁eset鏂規硶鏄綋璁懼涓㈠け鏃跺敮涓鏈夋晥鐨勬柟娉曪紝騫朵笖鏄簲鐢ㄧ▼搴忓彲鐢ㄦ潵鎶婅澶囦粠涓㈠け鐘舵佹仮澶嶅埌鎿嶄綔鐘舵佺殑鍞竴鏂規硶銆傞櫎闈炲簲鐢ㄧ▼搴忛噴鏀炬墍鏈夊湪D3DPOOL_DEFAULT涓垎閰嶇殑璧勬簮錛屽寘鎷敤IDirect3DDevice9::CreateRenderTarget鍜孖Direct3DDevice9::CreateDepthSstencilSurface鏂規硶鍒涘緩鐨勮祫婧愶紝鍚﹀垯Reset灝嗕細澶辮觸銆?br> Direct3D涓ぇ閮ㄥ垎琚綣佽皟鐢ㄧ殑鏂規硶涓嶈繑鍥炰換浣曞叧浜庤澶囨槸鍚﹀凡涓㈠け鐨勪俊鎭傚簲鐢ㄧ▼搴忓彲浠ョ戶緇皟鐢ㄦ覆鏌撴柟娉曪紝濡侷Direct3DDevice9: rawPrimitive錛岃屼笉浼氭敹鍒拌澶囦涪澶辯殑閫氱煡銆傚湪Direct3D鍐呴儴錛岃繖浜涙搷浣滆鎶涘純錛岀洿鍒拌澶囪閲嶇疆涓烘搷浣滅姸鎬佷負姝€?br> 閫氳繃鏌ヨIDirect3DDevice9::TestCooperativeLevel鏂規硶鐨勮繑鍥炲鹼紝搴旂敤紼嬪簭鍙互鍐沖畾鍦ㄩ亣鍒拌澶囦涪澶辨椂濡備綍澶勭悊銆?br> 綆$悊璧勬簮
璧勬簮綆$悊鏄皢璧勬簮浠庣郴緇熷唴瀛樻彁鍗囧埌璁懼鍙闂瓨鍌ㄥ櫒鍙婁粠璁懼鍙闂瓨鍌ㄥ櫒涓姏寮冪殑榪囩▼銆侻icrosoft? Direct3D?榪愯搴撴湁鑷繁鐨勫熀浜庢渶榪戞渶灝戜嬌鐢紙least-recently-used錛変紭鍏堢駭鎶鏈殑綆$悊綆楁硶銆傚綋Direct3D媯嫻嬪埌鍦ㄤ竴甯т腑鈥斺斿湪IDirect3DDevice9::BeginScene鍜孖Direct3DDevice9::EndScene璋冪敤涔嬮棿鈥斺旇澶囧彲璁塊棶鍐呭瓨鏃犳硶鍚屾椂瀛樺偍鎵鏈夎祫婧愭椂錛屽畠灝卞垏鎹㈠埌鏈榪戞渶澶氫嬌鐢紙most-recently-used錛変紭鍏堢駭鎶鏈?br> 鍦ㄥ垱寤烘椂浣跨敤D3DPOOL_MANAGED鏍囧織鎸囧畾涓涓敱緋葷粺綆$悊鐨勮祫婧愩傜敱緋葷粺綆$悊鐨勮祫婧愬湪璁懼鐨勪涪澶辯姸鎬佸拰鎿嶄綔鐘舵侀棿鐨勮漿鎹腑鎸佺畫瀛樺湪銆傞氳繃璋冪敤IDirect3DDevice9::Reset璁懼鍙互琚噸緗紝騫朵笖榪欑被璧勬簮鍙互緇х畫姝e父榪愪綔鑰屾棤闇閲嶆柊杞藉叆鍥劇墖銆備絾鏄紝濡傛灉璁懼蹇呴』琚攢姣佸拰閲嶅緩錛岄偅涔堟墍鏈夌敤D3DPOOL_MANAGED鍒涘緩鐨勮祫婧愪篃蹇呴』琚噸寤恒?br> 鍦ㄥ垱寤烘椂浣跨敤D3DPOOL_DEFAULT鏍囧織鎸囧畾鎶婅祫婧愭斁鍦ㄩ粯璁ょ殑姹犱腑銆傚湪榛樿鐨勬睜涓殑璧勬簮鍦ㄨ澶囦粠涓㈠け鐘舵佸埌鎿嶄綔鐘舵佺殑杞崲榪囩▼涓笉鎸佺畫瀛樺湪錛岃繖浜涜祫婧愬繀欏誨湪璋冪敤Reset涔嬪墠閲婃斁錛岀劧鍚庨噸寤恒?br> 鏇村鏈夊叧璁懼鐨勪涪澶辯姸鎬佺殑淇℃伅錛岃鍙傞槄涓㈠け鐨勮澶囥?br> 娉ㄦ剰涓嶆槸鎵鏈夌殑綾誨瀷鍜岀敤閫旈兘鏀寔璧勬簮綆$悊銆備緥濡傦紝鐢―3DUSAGE_RENDERTARGET鏍囧織鍒涘緩鐨勫璞′笉鏀寔璧勬簮綆$悊銆傚彟澶栵紝涓嶅緩璁闇瑕侀綣佹敼鍙樺叾鍐呭鐨勫璞′嬌鐢ㄨ祫婧愮鐞嗐備緥濡傦紝鍦ㄦ煇浜涚‖浠朵笂瀵逛竴涓瘡甯ч兘闇鏀瑰彉鐨勯《鐐圭紦瀛樿繘琛岃嚜鍔ㄧ鐞嗕細涓ラ噸闄嶄綆鎬ц兘銆備絾鏄紝瀵圭汗鐞嗚祫婧愭潵璇磋繖涓嶆槸涓涓棶棰樸?br>
渚嬪瓙錛堟憳鑷猚odesampler錛夛細
浠ヤ笅鍐呭涓虹▼搴忎唬鐮?
//----------------------------------------------------------------------------- // Name: invalidateDeviceObjects() // Desc: If the lost device can be restored, the application prepares the // device by destroying all video-memory resources and any // swap chains. This is typically accomplished by using the SAFE_RELEASE // macro. //----------------------------------------------------------------------------- HRESULT invalidateDeviceObjects( void ) { // // To see how mismanagement of an object''s reference counter can cause us // problems when calling Reset on the device, uncomment the line below. // The line below will call AddRef() on the vertex buffer object, which w // ill add one to the vertex buffer''s reference count. This will cause it // to hang around after we call Release() on it, which is not what we // wanted to happen here. // //g_pVertexBuffer->AddRef();
// // NOTE: You could use the SAFE_RELEASE macro to invalidate your device // objects like so: // SAFE_RELEASE( g_pTexture ) SAFE_RELEASE( g_pVertexBuffer ) SAFE_RELEASE( g_pTeapotMesh ) return S_OK; }
//----------------------------------------------------------------------------- // Name: restoreDeviceObjects() // Desc: You are encouraged to develop applications with a single code path to // respond to device loss. This code path is likely to be similar, if not // identical, to the code path taken to initialize the device at startup. //----------------------------------------------------------------------------- HRESULT restoreDeviceObjects( void ) { // // Set some important state settings... //
D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ), 640.0f / 480.0f, //(float)(g_d3dpp.BackBufferWidth / g_d3dpp.BackBufferHeight), 0.1f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
g_pd3dDevice->SetLight( 0, &g_pLight0 ); g_pd3dDevice->LightEnable( 0, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.2f, 0.2f, 0.2f, 1.0f ) );
// // Create a texture object... //
D3DXCreateTextureFromFile( g_pd3dDevice, "test.bmp", &g_pTexture );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
// // Create a vertex buffer... // // NOTE: When a device is lost, vertex buffers created using // D3DPOOL_DEFAULT must be released properly before calling // IDirect3DDevice9::Reset. //
g_pd3dDevice->CreateVertexBuffer( 4*sizeof(QuadVertex), D3DUSAGE_WRITEONLY, QuadVertex::FVF_Flags, //D3DPOOL_MANAGED, // Does not have to be properly Released before calling IDirect3DDevice9::Reset D3DPOOL_DEFAULT, // Must be Released properly before calling IDirect3DDevice9::Reset &g_pVertexBuffer, NULL ); void *pVertices = NULL;
g_pVertexBuffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 ); memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) ); g_pVertexBuffer->Unlock();
// // Create a mesh object... // // NOTE: When a device is lost, meshes created using D3DXMESH_DYNAMIC // must be released properly before calling IDirect3DDevice9::Reset. //
D3DXLoadMeshFromX( "teapot.x", //D3DXMESH_SYSTEMMEM, // Does not have to be properly Released before calling IDirect3DDevice9::Reset //D3DXMESH_MANAGED, // Does not have to be properly Released before calling IDirect3DDevice9::Reset //D3DXMESH_WRITEONLY, // Does not have to be properly Released before calling IDirect3DDevice9::Reset D3DXMESH_DYNAMIC, // Must be Released properly before calling IDirect3DDevice9::Reset g_pd3dDevice, NULL, NULL, NULL, NULL, &g_pTeapotMesh ); return S_OK; }
//----------------------------------------------------------------------------- // Name: render() // Desc: //----------------------------------------------------------------------------- void render( void ) { // // Before we render, we need to make sure we haven''t lost our device. // If we have, we''ll need to restore it before we can continue. //
HRESULT hr;
if( g_bDeviceLost == true ) { // Yield some CPU time to other processes Sleep( 100 ); // 100 milliseconds
// // Test the cooperative level to see if it''s okay to render. // The application can determine what to do on encountering a lost // device by querying the return value of the TestCooperativeLevel // method. //
if( FAILED( hr = g_pd3dDevice->TestCooperativeLevel() ) ) { // The device has been lost but cannot be reset at this time. // Therefore, rendering is not possible and we''ll have to return // and try again at a later time. if( hr == D3DERR_DEVICELOST ) return;
// The device has been lost but it can be reset at this time. if( hr == D3DERR_DEVICENOTRESET ) { // // If the device can be restored, the application prepares the // device by destroying all video-memory resources and any // swap chains. //
invalidateDeviceObjects();
// // Then, the application calls the Reset method. // // Reset is the only method that has an effect when a device // is lost, and is the only method by which an application can // change the device from a lost to an operational state. // Reset will fail unless the application releases all // resources that are allocated in D3DPOOL_DEFAULT, including // those created by the IDirect3DDevice9::CreateRenderTarget // and IDirect3DDevice9::CreateDepthStencilSurface methods. //
hr = g_pd3dDevice->Reset( &g_d3dpp );
if( FAILED(hr ) ) return;
// // Finally, a lost device must re-create resources (including // video memory resources) after it has been reset. //
restoreDeviceObjects(); }
return; }
g_bDeviceLost = false; }
// // Render a teapot and textured quad... //
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.35f,0.53f,0.7f,1.0f), 1.0f, 0 );
g_pd3dDevice->BeginScene(); { D3DXMATRIX matView; D3DXMATRIX matWorld; D3DXMATRIX matRotation; D3DXMATRIX matTranslation;
D3DXMatrixIdentity( &matView ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// Place and render first teapot... D3DXMatrixRotationYawPitchRoll( &matRotation, D3DXToRadian(g_fSpinX), D3DXToRadian(g_fSpinY), 0.0f ); D3DXMatrixTranslation( &matTranslation, 1.5f, 0.0f, 6.0f ); matWorld = matRotation * matTranslation; g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
g_pd3dDevice->SetMaterial( &g_teapotMtrl ); g_pTeapotMesh->DrawSubset(0);
// Place and render textured quad... D3DXMatrixTranslation( &matTranslation, -1.5f, 0.0f, 6.0f ); matWorld = matRotation * matTranslation; g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
g_pd3dDevice->SetMaterial( &g_quadMtrl ); g_pd3dDevice->SetTexture( 0, g_pTexture ); g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(QuadVertex) ); g_pd3dDevice->SetFVF( QuadVertex::FVF_Flags ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); } g_pd3dDevice->EndScene();
// // If Present fails with D3DERR_DEVICELOST the application needs to be // notified so it cleanup resources and reset the device. //
hr = g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
if( g_bHandleLostDevice == true ) { if( hr == D3DERR_DEVICELOST ) g_bDeviceLost = true; } } |
|
|