//-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9CreateDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D10DestroyDevice(); g_SettingsDlg.OnD3D10DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); g_Terrain.OnDestroyDevice(); g_BallMesh.Destroy(); g_SkyMesh.Destroy(); SAFE_RELEASE( g_pFont10 ); SAFE_RELEASE( g_pSprite10 ); SAFE_DELETE( g_pTxtHelper ); SAFE_RELEASE( g_pEffect10 ); SAFE_RELEASE( g_pBasicDecl10 ); SAFE_RELEASE( g_pBallDecl10 ); SAFE_RELEASE( g_pGrassDecl10 ); SAFE_RELEASE( g_pHeightTexRV ); SAFE_RELEASE( g_pNormalTexRV ); SAFE_RELEASE( g_pGrassTexRV ); SAFE_RELEASE( g_pDirtTexRV ); SAFE_RELEASE( g_pGroundGrassTexRV ); SAFE_RELEASE( g_pMaskTexRV ); SAFE_RELEASE( g_pShadeNormalTexRV ); SAFE_RELEASE( g_pStreamDataVB10 ); SAFE_RELEASE( g_pGrassDataVB10 ); }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D11DestroyDevice(); g_D3DSettingsDlg.OnD3D11DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_DELETE( g_pTxtHelper ); SAFE_DELETE( g_pTxtHelper1 ); g_SceneMesh.Destroy(); g_Poles.Destroy(); SAFE_RELEASE( g_pSceneVS ); SAFE_RELEASE( g_pScenePS ); SAFE_RELEASE( g_pSM_VS ); SAFE_RELEASE( g_pcbConstants ); SAFE_RELEASE( g_pRSMDepthStencilTexture ); SAFE_RELEASE( g_pDepthStencilTextureDSV ); SAFE_RELEASE( g_pDepthTextureSRV ); SAFE_RELEASE( g_pSceneVertexLayout ); SAFE_RELEASE( g_pBlendStateNoBlend ); SAFE_RELEASE( g_pBlendStateColorWritesOff ); SAFE_RELEASE( g_pSamplePoint ); SAFE_RELEASE( g_pSamplePointCmp ); SAFE_RELEASE( g_pSampleLinear ); }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice(void* /*pUserContext*/) { g_MeshPowerPlant.Destroy(); g_MeshTestScene.Destroy(); DestroyD3DComponents(); }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice( void* pUserContext ) { UNREFERENCED_PARAMETER(pUserContext); g_MeshPowerPlant.Destroy(); g_TeapotMesh.Destroy(); DestroyD3DComponents(); }
//-------------------------------------------------------------------------------------- // 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 = S_OK; V_RETURN(g_MeshPowerPlant.Create(pd3dDevice, L"powerplant\\powerplant.sdkmesh")); V_RETURN(g_MeshTestScene.Create(pd3dDevice, L"ShadowColumns\\testscene.sdkmesh")); g_pSelectedMesh = &g_MeshPowerPlant; return CreateD3DComponents(pd3dDevice); }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependent on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { UNREFERENCED_PARAMETER(pBackBufferSurfaceDesc); UNREFERENCED_PARAMETER(pUserContext); HRESULT hr = S_OK; V_RETURN( g_MeshPowerPlant.Create( pd3dDevice, L"powerplant\\powerplant.sdkmesh" ) ); V_RETURN( g_TeapotMesh.Create( pd3dDevice, L"teapot\\teapot.sdkmesh" ) ); return CreateD3DComponents( pd3dDevice ); }
void App::RenderGBuffer(ID3D11DeviceContext* d3dDeviceContext, CDXUTSDKMesh& mesh_opaque, CDXUTSDKMesh& mesh_alpha, const CFirstPersonCamera* viewerCamera, const D3D11_VIEWPORT* viewport, const UIConstants* ui) { // Clear GBuffer // NOTE: We actually only need to clear the depth buffer here since we replace unwritten (i.e. far plane) samples // with the skybox. We use the depth buffer to reconstruct position and only in-frustum positions are shaded. // NOTE: Complementary Z buffer: clear to 0 (far)! d3dDeviceContext->ClearDepthStencilView(mDepthBuffer->GetDepthStencil(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 0.0f, 0); d3dDeviceContext->IASetInputLayout(mMeshVertexLayout); d3dDeviceContext->VSSetConstantBuffers(0, 1, &mPerFrameConstants); d3dDeviceContext->VSSetShader(mGeometryVS->GetShader(), 0, 0); d3dDeviceContext->GSSetShader(0, 0, 0); d3dDeviceContext->RSSetViewports(1, viewport); d3dDeviceContext->PSSetConstantBuffers(0, 1, &mPerFrameConstants); d3dDeviceContext->PSSetSamplers(0, 1, &mDiffuseSampler); // Diffuse texture set per-material by DXUT mesh routines // Set up render GBuffer render targets d3dDeviceContext->OMSetDepthStencilState(mDepthState, 0); d3dDeviceContext->OMSetRenderTargets(static_cast<UINT>(mGBufferRTV.size()), &mGBufferRTV.front(), mDepthBuffer->GetDepthStencil()); d3dDeviceContext->OMSetBlendState(mGeometryBlendState, 0, 0xFFFFFFFF); // Render opaque geometry if (mesh_opaque.IsLoaded()) { d3dDeviceContext->RSSetState(mRasterizerState); d3dDeviceContext->PSSetShader(mGBufferPS->GetShader(), 0, 0); mesh_opaque.Render(d3dDeviceContext, 0); } // Render alpha tested geometry if (mesh_alpha.IsLoaded()) { d3dDeviceContext->RSSetState(mDoubleSidedRasterizerState); d3dDeviceContext->PSSetShader(mGBufferAlphaTestPS->GetShader(), 0, 0); mesh_alpha.Render(d3dDeviceContext, 0); } // Cleanup (aka make the runtime happy) d3dDeviceContext->OMSetRenderTargets(0, 0, 0); }
//-------------------------------------------------------------------------------------- // Release D3D10 resources created in OnD3D10CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D10DestroyDevice(); g_D3DSettingsDlg.OnD3D10DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_RELEASE( g_pFont ); SAFE_RELEASE( g_pSprite ); SAFE_DELETE( g_pTxtHelper ); SAFE_RELEASE( g_pVertexLayout ); SAFE_RELEASE( g_pEffect ); g_Mesh.Destroy(); }
//-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9ResetDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9LostDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9LostDevice(); g_SettingsDlg.OnD3D9LostDevice(); DXUTGetGlobalResourceCache().OnLostDevice(); if( g_pFont9 ) g_pFont9->OnLostDevice(); if( g_pEffect9 ) g_pEffect9->OnLostDevice(); SAFE_RELEASE( g_pSprite9 ); SAFE_DELETE( g_pTxtHelper ); g_Mesh.Destroy(); SAFE_RELEASE( g_pTexture9 ); }
//-------------------------------------------------------------------------------------- // Release D3D10 resources created in OnD3D10CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D10DestroyDevice(); g_D3DSettingsDlg.OnD3D10DestroyDevice(); CDXUTDirectionWidget::StaticOnD3D10DestroyDevice(); SAFE_RELEASE( g_pFont10 ); SAFE_RELEASE( g_pSprite10 ); SAFE_RELEASE( g_pEffect10 ); SAFE_RELEASE( g_pVertexLayout ); g_Mesh.Destroy(); SAFE_RELEASE( g_pMeshTexRV ); }
//-------------------------------------------------------------------------------------- // Release D3D10 resources created in OnD3D10CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D10DestroyDevice(); g_D3DSettingsDlg.OnD3D10DestroyDevice(); CDXUTDirectionWidget::StaticOnD3D10DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_RELEASE( g_pEffect ); SAFE_RELEASE( g_pFont10 ); SAFE_RELEASE( g_pSprite10 ); SAFE_DELETE( g_pTxtHelper ); SAFE_RELEASE( g_pStaticVertexLayout ); SAFE_RELEASE( g_pSkinnedVertexLayout ); g_SceneMesh.Destroy(); g_FanMesh.Destroy(); for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) g_pLinkedMeshes[iMesh].Destroy(); SAFE_DELETE_ARRAY( g_pLinkedMeshes ); g_AnimMesh.Destroy(); }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D11DestroyDevice(); g_D3DSettingsDlg.OnD3D11DestroyDevice(); CDXUTDirectionWidget::StaticOnD3D11DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_DELETE( g_pTxtHelper ); g_Mesh11.Destroy(); SAFE_RELEASE( g_pEffect ); SAFE_RELEASE( g_pVertexLayout11 ); SAFE_RELEASE( g_pVertexBuffer ); SAFE_RELEASE( g_pIndexBuffer ); SAFE_RELEASE( g_pEnvironmentMapSRV ); }
//-------------------------------------------------------------------------------------- // RenderSky //-------------------------------------------------------------------------------------- void RenderSky( ID3D10Device* pd3dDevice ) { D3DXMATRIX mWorld; D3DXVECTOR3 vEye; D3DXVECTOR3 vDir; D3DXMATRIX mCamWorld; D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMatrixRotationY( &mWorld, -D3DX_PI / 2.5f ); GetCameraData( &mCamWorld, &mView, &mProj, &vEye, &vDir ); mView._41 = mView._42 = mView._43 = 0.0f; D3DXMATRIX mWVP = mWorld * mCamWorld * mView * mProj; g_pmWorldViewProj->SetMatrix( ( float* )&mWVP ); g_pmWorld->SetMatrix( ( float* )&mWorld ); pd3dDevice->IASetInputLayout( g_pBasicDecl10 ); g_SkyMesh.Render( pd3dDevice, g_pRenderSky, g_ptxDiffuse ); }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { HRESULT hr = S_OK; struct OLD_VERT { D3DXVECTOR3 Pos; D3DXVECTOR3 Norm; D3DXVECTOR2 Tex; }; V_RETURN( g_Mesh.Create( pd3dDevice, L"rook.sdkmesh", true ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"rook_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr = S_OK; // 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; } D3DXMATRIX mWorld = *g_Camera.GetWorldMatrix(); D3DXMATRIX mView = *g_Camera.GetViewMatrix(); D3DXMATRIX mProj = *g_Camera.GetProjMatrix(); D3DXMATRIX mWorldViewProjection = mWorld * mView * mProj; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 160, 160, 250 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { g_pEffect9->SetMatrix( g_hmWorld, &mWorld ); g_pEffect9->SetMatrix( g_hmWorldViewProjection, &mWorldViewProjection ); g_pEffect9->SetTexture( g_htxDiffuse, g_pTexture9 ); pd3dDevice->SetVertexDeclaration( g_pDecl9 ); g_Mesh.Render( pd3dDevice, g_pEffect9, g_hRenderScene ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); // These events are to help PIX identify what the code is doing RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); DXUT_EndPerfEvent(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) // or that are tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont9 ) V_RETURN( g_pFont9->OnResetDevice() ); if( g_pEffect9 ) V_RETURN( g_pEffect9->OnResetDevice() ); V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite9 ) ); g_pTxtHelper = new CDXUTTextHelper( g_pFont9, g_pSprite9, NULL, NULL, 15 ); // 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_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350 ); g_SampleUI.SetSize( 170, 300 ); // load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"misc\\ball.sdkmesh" ) ); // Load the texture WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"misc\\seafloor.dds" ) ); V_RETURN( CreateDDSTextureFromFile( pd3dDevice, str, &g_pTexture9 ) ); if( DXUTIsWindowed() ) g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( true ); else g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( false ); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // Clear the render target float ClearColor[4] = { 0.9569f, 0.9569f, 1.0f, 0.0f }; ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMATRIX mWorldView; D3DXMATRIX mWorldViewProj; mWorld = *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldView = mWorld * mView; mWorldViewProj = mWorldView * mProj; // Set variables g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj ); g_pmWorldView->SetMatrix( ( float* )&mWorldView ); g_pmWorld->SetMatrix( ( float* )&mWorld ); g_pmProj->SetMatrix( ( float* )&mProj ); g_pDiffuseTex->SetResource( g_pMeshTexRV ); D3DXVECTOR3 lightDir( -1,1,-1 ); D3DXVECTOR3 viewLightDir; D3DXVec3TransformNormal( &viewLightDir, &lightDir, &mView ); D3DXVec3Normalize( &viewLightDir, &viewLightDir ); g_pViewSpaceLightDir->SetFloatVector( ( float* )&viewLightDir ); // Get VB and IB UINT offset = 0; UINT stride = g_Mesh.GetVertexStride( 0, 0 ); ID3D10Buffer* pVB = g_Mesh.GetVB10( 0, 0 ); ID3D10Buffer* pIB = g_Mesh.GetAdjIB10( 0 ); // Set Input Assembler params pd3dDevice->IASetInputLayout( g_pVertexLayout ); pd3dDevice->IASetIndexBuffer( pIB, g_Mesh.GetIBFormat10( 0 ), 0 ); pd3dDevice->IASetVertexBuffers( 0, 1, &pVB, &stride, &offset ); pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ ); // Render using the technique g_pRenderTextured SDKMESH_SUBSET* pSubset = NULL; D3D10_TECHNIQUE_DESC techDesc; g_pRenderTextured->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; p++ ) { g_pRenderTextured->GetPassByIndex( p )->Apply( 0 ); for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); subset++ ) { pSubset = g_Mesh.GetSubset( 0, subset ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount * 2, ( UINT )pSubset->IndexStart, ( UINT )pSubset->VertexStart ); } } // Render the chess piece just for show // Render using the technique g_pRenderPiece g_pRenderPiece->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; p++ ) { g_pRenderPiece->GetPassByIndex( p )->Apply( 0 ); for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); subset++ ) { pSubset = g_Mesh.GetSubset( 0, subset ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount * 2, ( UINT )pSubset->IndexStart, ( UINT )pSubset->VertexStart ); } } }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D11 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; // If the settings dialog is being shown, then render it instead of rendering the app's scene if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } // Clear the render target and depth stencil auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue ); auto pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); // Get the projection & view matrix from the camera class XMMATRIX mWorld = g_Camera.GetWorldMatrix(); XMMATRIX mProj = g_Camera.GetProjMatrix(); XMMATRIX mView = g_Camera.GetViewMatrix(); // Get the light direction XMVECTOR vLightDir = g_LightControl.GetLightDirection(); // Render the light arrow so the user can visually see the light dir V( g_LightControl.OnRender( Colors::Yellow, mView, mProj, g_Camera.GetEyePt() ) ); // Ambient Light static const XMVECTORF32 s_vLightColorA = { 0.1f, 0.1f, 0.1f, 1.0f }; g_pAmbientLightColor->SetFloatVector( s_vLightColorA ); g_pAmbientLightEnable->SetBool(true); // Hemi Ambient Light static const XMVECTORF32 s_vLightColorH1 = { 0.3f, 0.3f, 0.4f, 1.0f }; g_pHemiAmbientLightColor->SetFloatVector( s_vLightColorH1 ); g_pHemiAmbientLightEnable->SetBool(true); XMFLOAT4 vLightGrndClr( 0.05f, 0.05f, 0.05f, 1.f ); g_pHemiAmbientLightGroundColor->SetFloatVector( reinterpret_cast<float*>( &vLightGrndClr ) ); XMFLOAT4 vVec(0.0f, 1.0f, 0.0f, 1.0f); g_pHemiAmbientLightDirUp->SetFloatVector( reinterpret_cast<float*>( &vVec ) ); // Directional Light g_pDirectionalLightColor->SetFloatVector( Colors::White ); g_pDirectionalLightEnable->SetBool(true); XMFLOAT4 tmp; XMStoreFloat4( &tmp, vLightDir ); tmp.w = 1.f; g_pDirectionalLightDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) ); // Environment Light - color comes from the texture g_pEnvironmentLightColor->SetFloatVector( Colors::Black ); g_pEnvironmentLightEnable->SetBool(true); // Setup the Eye based on the DXUT camera XMVECTOR vEyePt = g_Camera.GetEyePt(); XMVECTOR vDir = g_Camera.GetLookAtPt() - vEyePt; XMStoreFloat4( &tmp, vDir ); tmp.w = 1.f; g_pEyeDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) ); //Get the mesh //IA setup pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 ); UINT Strides[1]; UINT Offsets[1]; ID3D11Buffer* pVB[1]; pVB[0] = g_Mesh11.GetVB11( 0, 0 ); Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dImmediateContext->IASetIndexBuffer( g_Mesh11.GetIB11( 0 ), g_Mesh11.GetIBFormat11( 0 ), 0 ); // Set the per object constant data XMMATRIX mWorldViewProjection = mWorld * mView * mProj; // VS Per object XMFLOAT4X4 tmp4x4; XMStoreFloat4x4( &tmp4x4, mWorldViewProjection ); g_pWorldViewProjection->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) ); XMStoreFloat4x4( &tmp4x4, mWorld ); g_pWorld->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) ); // Setup the Shader Linkage based on the user settings for Lighting ID3DX11EffectClassInstanceVariable* pLightClassVar; // Ambient Lighting First - Constant or Hemi? if ( g_bHemiAmbientLighting ) { pLightClassVar = g_pHemiAmbientLightClass; } else { pLightClassVar = g_pAmbientLightClass; } if (g_pAmbientLightIface) { g_pAmbientLightIface->SetClassInstance(pLightClassVar); } // Direct Light - None or Directional if (g_bDirectLighting) { pLightClassVar = g_pDirectionalLightClass; } else { // Disable ALL Direct Lighting pLightClassVar = g_pAmbientLightClass; } if (g_pDirectionalLightIface) { g_pDirectionalLightIface->SetClassInstance(pLightClassVar); } // Setup the selected material class instance E_MATERIAL_TYPES iMaterialTech = g_iMaterial; switch( g_iMaterial ) { case MATERIAL_PLASTIC: case MATERIAL_PLASTIC_TEXTURED: // Bind the Environment light for reflections pLightClassVar = g_pEnvironmentLightClass; if (g_bLightingOnly) { iMaterialTech = MATERIAL_PLASTIC_LIGHTING_ONLY; } break; case MATERIAL_ROUGH: case MATERIAL_ROUGH_TEXTURED: // UnBind the Environment light pLightClassVar = g_pAmbientLightClass; if (g_bLightingOnly) { iMaterialTech = MATERIAL_ROUGH_LIGHTING_ONLY; } break; } if (g_pEnvironmentLightIface) { g_pEnvironmentLightIface->SetClassInstance(pLightClassVar); } ID3DX11EffectTechnique* pTechnique = g_pTechnique; if (g_pMaterialIface) { #if USE_BIND_INTERFACES // We're using the techniques with pre-bound materials, // so select the appropriate technique. pTechnique = g_MaterialClasses[ iMaterialTech ].pTechnique; #else // We're using a single technique and need to explicitly // bind a concrete material instance. g_pMaterialIface->SetClassInstance( g_MaterialClasses[ iMaterialTech ].pClass ); #endif } // PS Per Prim // Shiny Plastic XMFLOAT3 clr1(1, 0, 0.5f); g_MaterialClasses[MATERIAL_PLASTIC].pColor->SetFloatVector( reinterpret_cast<float*>( &clr1 ) ); g_MaterialClasses[MATERIAL_PLASTIC].pSpecPower->SetInt(255); // Shiny Plastic with Textures XMFLOAT3 clr2(1, 0, 0.5f); g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr2 ) ); g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pSpecPower->SetInt(128); // Lighting Only Plastic XMFLOAT3 clr3(1, 1, 1); g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr3 ) ); g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pSpecPower->SetInt(128); // Rough Material XMFLOAT3 clr4(0, 0.5f, 1); g_MaterialClasses[MATERIAL_ROUGH].pColor->SetFloatVector( reinterpret_cast<float*>( &clr4 ) ); g_MaterialClasses[MATERIAL_ROUGH].pSpecPower->SetInt(6); // Rough Material with Textures XMFLOAT3 clr5(0, 0.5f, 1); g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr5 ) ); g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pSpecPower->SetInt(6); // Lighting Only Rough XMFLOAT3 clr6(1, 1, 1); g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr6 ) ); g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pSpecPower->SetInt(6); if (g_bWireFrame) g_pFillMode->SetInt(1); else g_pFillMode->SetInt(0); // Apply the technique to update state. pTechnique->GetPassByIndex(0)->Apply(0, pd3dImmediateContext); //Render g_Mesh11.Render( pd3dImmediateContext, 0, 1, INVALID_SAMPLER_SLOT); // Tell the UI items to render DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { g_pd3dDevice = pd3dDevice; HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); D3DXVECTOR3 vCenter( 0.25767413f, -28.503521f, 111.00689f); FLOAT fObjectRadius = 378.15607f; D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x, -vCenter.y, -vCenter.z ); D3DXMATRIXA16 m; D3DXMatrixRotationY( &m, D3DX_PI ); g_mCenterMesh *= m; D3DXMatrixRotationX( &m, D3DX_PI / 2.0f ); g_mCenterMesh *= m; // Compile the shaders to a model based on the feature level we acquired ID3DBlob* pVertexShaderBuffer = NULL; ID3DBlob* pGeometryShaderBuffer = NULL; ID3DBlob* pPixelShaderBuffer = NULL; switch( DXUTGetD3D11DeviceFeatureLevel() ) { case D3D_FEATURE_LEVEL_11_0: V_RETURN( CompileShaderFromFile( L"cloth_renderer_VS.hlsl", "VSMain", "vs_5_0" , &pVertexShaderBuffer ) ); V_RETURN( CompileShaderFromFile( L"cloth_renderer_PS.hlsl", "GSMain", "gs_5_0" , &pGeometryShaderBuffer ) ); V_RETURN( CompileShaderFromFile( L"cloth_renderer_PS.hlsl", "PSMain", "ps_5_0" , &pPixelShaderBuffer ) ); break; } // Create the shaders V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader ) ); V_RETURN( pd3dDevice->CreateGeometryShader( pGeometryShaderBuffer->GetBufferPointer(), pGeometryShaderBuffer->GetBufferSize(), NULL, &g_pGeometryShader ) ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader ) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &g_pVertexLayout11 ) ); SAFE_RELEASE( pVertexShaderBuffer ); SAFE_RELEASE( pPixelShaderBuffer ); SAFE_RELEASE( pGeometryShaderBuffer ); // Load the mesh V_RETURN( g_Mesh11.Create( pd3dDevice, L"tiny\\tiny.sdkmesh", true ) ); // Create a sampler state D3D11_SAMPLER_DESC SamDesc; SamDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.MipLODBias = 0.0f; SamDesc.MaxAnisotropy = 1; SamDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; SamDesc.BorderColor[0] = SamDesc.BorderColor[1] = SamDesc.BorderColor[2] = SamDesc.BorderColor[3] = 0; SamDesc.MinLOD = 0; SamDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &g_pSamLinear ) ); // Setup constant buffers D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( CB_VS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbVSPerObject ) ); Desc.ByteWidth = sizeof( CB_PS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbPSPerObject ) ); Desc.ByteWidth = sizeof( CB_PS_PER_FRAME ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbPSPerFrame ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 30.0f, 30.0f, -80.0f ); D3DXVECTOR3 vecAt ( 10.0f, 20.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); cloths.resize(numFlags); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].create_buffers(clothWidth, clothHeight); } initBullet(); std::wstring flagTexsName[] = { L"atiFlag.bmp", L"amdFlag.bmp", }; int numFlagTexs = 2; WCHAR flagTexs[2][MAX_PATH]; HRESULT res = DXUTFindDXSDKMediaFileCch(flagTexs[0],MAX_PATH, flagTexsName[0].c_str()); res = DXUTFindDXSDKMediaFileCch(flagTexs[1],MAX_PATH, flagTexsName[1].c_str()); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].create_texture(flagTexs[flagIndex % numFlagTexs]); cloths[flagIndex].x_offset = 0; cloths[flagIndex].y_offset = 0; cloths[flagIndex].z_offset = 0; } my_capsule.create_buffers(50,40); my_capsule.create_texture(); //Turn off backface culling D3D11_RASTERIZER_DESC rsDesc; ZeroMemory(&rsDesc,sizeof(D3D11_RASTERIZER_DESC) ); rsDesc.CullMode = D3D11_CULL_NONE; rsDesc.FillMode = D3D11_FILL_SOLID; hr = pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerState); rsDesc.FillMode = D3D11_FILL_WIREFRAME; hr = pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerStateWF); SAFE_RELEASE(pd3dImmediateContext); return S_OK; }
//-------------------------------------------------------------------------------------- // render callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ) { static int s_iCounter = 0; // If the settings dialog is being shown, then render it instead of rendering the app's scene if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } if( g_pScenePS == NULL && s_iCounter == 0 ) { s_iCounter = 4; } if( s_iCounter > 0 ) s_iCounter --; if( s_iCounter == 1 && g_pScenePS == NULL ) { HRESULT hr = S_OK; // Create the shaders ID3DBlob* pBlob = NULL; // VS hr = CompileShaderFromFile( L"ContactHardeningShadows11.hlsl", "VS_RenderScene", "vs_5_0", &pBlob ); hr = pd3dDevice->CreateVertexShader( pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &g_pSceneVS ); DXUT_SetDebugName( g_pSceneVS, "VS_RenderScene" ); // Define our scene vertex data layout const D3D11_INPUT_ELEMENT_DESC SceneLayout[] = { { "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 }, { "TEXTURE", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; hr = pd3dDevice->CreateInputLayout( SceneLayout, ARRAYSIZE( SceneLayout ), pBlob->GetBufferPointer(), pBlob->GetBufferSize(), &g_pSceneVertexLayout ); SAFE_RELEASE( pBlob ); DXUT_SetDebugName( g_pSceneVertexLayout, "SceneLayout" ); hr = CompileShaderFromFile( L"ContactHardeningShadows11.hlsl", "VS_RenderSceneSM", "vs_5_0", &pBlob ); hr = pd3dDevice->CreateVertexShader( pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &g_pSM_VS ); SAFE_RELEASE( pBlob ); DXUT_SetDebugName( g_pSM_VS, "VS_RenderSceneSM" ); // PS hr = CompileShaderFromFile( L"ContactHardeningShadows11.hlsl", "PS_RenderScene", "ps_5_0", &pBlob ); hr = pd3dDevice->CreatePixelShader( pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &g_pScenePS ); SAFE_RELEASE( pBlob ); DXUT_SetDebugName( g_pScenePS, "PS_RenderScene" ); s_iCounter = 0; } else if( g_pScenePS != NULL ) { ID3D11RenderTargetView* pRTV[2] = { NULL,NULL }; ID3D11ShaderResourceView* pSRV[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Array of our samplers ID3D11SamplerState* ppSamplerStates[3] = { g_pSamplePoint, g_pSampleLinear, g_pSamplePointCmp }; pd3dImmediateContext->PSSetSamplers( 0, 3, ppSamplerStates ); // Store off original render target, this is the back buffer of the swap chain ID3D11RenderTargetView* pOrigRTV = DXUTGetD3D11RenderTargetView(); ID3D11DepthStencilView* pOrigDSV = DXUTGetD3D11DepthStencilView(); // Clear the render target float ClearColor[4] = { 0.0f, 0.25f, 0.25f, 0.55f }; pd3dImmediateContext->ClearRenderTargetView( DXUTGetD3D11RenderTargetView(), ClearColor ); pd3dImmediateContext->ClearDepthStencilView( DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0 ); // Get the projection & view matrix from the camera class D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mViewProjLight; D3DXMATRIXA16 mWorldViewProjection; D3DXVECTOR3 vLightDir; // disable color writes pd3dImmediateContext->OMSetBlendState(g_pBlendStateColorWritesOff, 0, 0xffffffff); RenderShadowMap( pd3dDevice, pd3dImmediateContext, mViewProjLight, vLightDir ); // enable color writes pd3dImmediateContext->OMSetBlendState(g_pBlendStateNoBlend, 0, 0xffffffff); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mWorldViewProjection = mView * mProj; // Setup the constant buffer for the scene vertex shader D3D11_MAPPED_SUBRESOURCE MappedResource; pd3dImmediateContext->Map( g_pcbConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ); CB_CONSTANTS* pConstants = ( CB_CONSTANTS* )MappedResource.pData; D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjection, &mWorldViewProjection ); D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjLight, &mViewProjLight ); pConstants->vShadowMapDimensions = D3DXVECTOR4(g_fShadowMapWidth, g_fShadowMapHeight, 1.0f/g_fShadowMapWidth, 1.0f/g_fShadowMapHeight); pConstants->vLightDir = D3DXVECTOR4( vLightDir.x, vLightDir.y, vLightDir.z, 0.0f ); pConstants->fSunWidth = g_fSunWidth; pd3dImmediateContext->Unmap( g_pcbConstants, 0 ); pd3dImmediateContext->VSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); pd3dImmediateContext->PSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); // Set the shaders pd3dImmediateContext->VSSetShader( g_pSceneVS, NULL, 0 ); pd3dImmediateContext->PSSetShader( g_pScenePS, NULL, 0 ); // Set the vertex buffer format pd3dImmediateContext->IASetInputLayout( g_pSceneVertexLayout ); // Rebind to original back buffer and depth buffer pRTV[0] = pOrigRTV; pd3dImmediateContext->OMSetRenderTargets(1, pRTV, pOrigDSV ); // set the shadow map pd3dImmediateContext->PSSetShaderResources( 1, 1, &g_pDepthTextureSRV ); // Render the scene g_SceneMesh.Render( pd3dImmediateContext, 0 ); g_Poles.Render( pd3dImmediateContext, 0 ); // restore resources pd3dImmediateContext->PSSetShaderResources( 0, 8, pSRV ); } // Render GUI DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); if( g_bGuiVisible ) { g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); } RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // Renders the depth only shadow map //-------------------------------------------------------------------------------------- void RenderShadowMap( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, D3DXMATRIXA16& mViewProjLight, D3DXVECTOR3& vLightDir ) { D3D11_RECT oldrects[1]; D3D11_VIEWPORT oldvp[2]; UINT num = 1; pd3dImmediateContext->RSGetScissorRects( &num, oldrects ); num = 1; pd3dImmediateContext->RSGetViewports( &num, oldvp ); oldvp[ 1 ] = oldvp[ 0 ]; D3D11_RECT rects[1] = { { 0, UINT(g_fShadowMapWidth), 0, UINT(g_fShadowMapHeight) } }; pd3dImmediateContext->RSSetScissorRects( 1, rects ); D3D11_VIEWPORT vp[1] = { { 0, 0, g_fShadowMapWidth, g_fShadowMapHeight, 0.0f, 1.0f } }; pd3dImmediateContext->RSSetViewports( 1, vp ); // Set our scene render target & keep original depth buffer ID3D11RenderTargetView* pRTVs[2] = {0,0}; pd3dImmediateContext->OMSetRenderTargets( 2, pRTVs, g_pDepthStencilTextureDSV ); // Clear the render target pd3dImmediateContext->ClearDepthStencilView( g_pDepthStencilTextureDSV, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0 ); // Get the projection & view matrix from the camera class D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXVECTOR3 up(0,1,0); D3DXVECTOR4 vLight( 0.0f,0.0f,0.0f, 1.0f ); D3DXVECTOR4 vLightLookAt4d( 0.0f,-0.5f,1.0f, 0.0f ); vLightLookAt4d = vLight + vLightLookAt4d; D3DXVec4Transform( &vLight, &vLight, g_LCamera.GetWorldMatrix() ); D3DXVec4Transform( &vLightLookAt4d, &vLightLookAt4d, g_LCamera.GetWorldMatrix() ); D3DXMatrixOrthoOffCenterLH( &mProj, -8.5, 9, -15, 11, -20, 20 ); D3DXVECTOR3 vLight3d( vLight.x, vLight.y, vLight.z ); D3DXVECTOR3 vLightLookAt3d( vLightLookAt4d.x, vLightLookAt4d.y, vLightLookAt4d.z ); vLightDir = vLightLookAt3d - vLight3d; D3DXMatrixLookAtLH( &mView, &vLight3d, &vLightLookAt3d, &up); mViewProjLight = mView * mProj; // Setup the constant buffer for the scene vertex shader D3D11_MAPPED_SUBRESOURCE MappedResource; pd3dImmediateContext->Map( g_pcbConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ); CB_CONSTANTS* pConstants = ( CB_CONSTANTS* )MappedResource.pData; D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjection, &mViewProjLight ); D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjLight, &mViewProjLight ); pConstants->vShadowMapDimensions = D3DXVECTOR4(g_fShadowMapWidth, g_fShadowMapHeight, 1.0f/g_fShadowMapWidth, 1.0f/g_fShadowMapHeight); pd3dImmediateContext->Unmap( g_pcbConstants, 0 ); pd3dImmediateContext->VSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); pd3dImmediateContext->PSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); // Set the shaders pd3dImmediateContext->VSSetShader( g_pSM_VS, NULL, 0 ); pd3dImmediateContext->PSSetShader( NULL, NULL, 0 ); // Set the vertex buffer format pd3dImmediateContext->IASetInputLayout( g_pSceneVertexLayout ); // Render the scene g_Poles.Render( pd3dImmediateContext, 0 ); // reset the old viewport etc. pd3dImmediateContext->RSSetScissorRects( 1, oldrects ); pd3dImmediateContext->RSSetViewports( 1, oldvp ); }
//-------------------------------------------------------------------------------------- // 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_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); g_pTxtHelper1 = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 35 ); // textures / rts D3D11_TEXTURE2D_DESC TDesc; TDesc.Width = UINT(g_fShadowMapWidth); TDesc.Height = UINT(g_fShadowMapHeight); TDesc.MipLevels = 1; TDesc.ArraySize = 1; TDesc.Format = DXGI_FORMAT_R16_TYPELESS; TDesc.SampleDesc.Count = 1; TDesc.SampleDesc.Quality = 0; TDesc.Usage = D3D11_USAGE_DEFAULT; TDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; TDesc.CPUAccessFlags = 0; TDesc.MiscFlags = 0; pd3dDevice->CreateTexture2D( &TDesc, 0, &g_pRSMDepthStencilTexture); DXUT_SetDebugName( g_pRSMDepthStencilTexture, "RSM" ); D3D11_DEPTH_STENCIL_VIEW_DESC DSVDesc; DSVDesc.Format = DXGI_FORMAT_D16_UNORM; DSVDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; DSVDesc.Flags = 0; DSVDesc.Texture2D.MipSlice = 0; pd3dDevice->CreateDepthStencilView( g_pRSMDepthStencilTexture, &DSVDesc, & g_pDepthStencilTextureDSV ); DXUT_SetDebugName( g_pDepthStencilTextureDSV, "RSM DSV" ); D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; SRVDesc.Format = DXGI_FORMAT_R16_UNORM; SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; SRVDesc.Texture2D.MipLevels = 1; SRVDesc.Texture2D.MostDetailedMip = 0; pd3dDevice->CreateShaderResourceView( g_pRSMDepthStencilTexture, &SRVDesc, &g_pDepthTextureSRV ); DXUT_SetDebugName( g_pDepthTextureSRV, "RSM SRV" ); // Setup constant buffers D3D11_BUFFER_DESC Desc; // Utility Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( CB_CONSTANTS ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbConstants ) ); DXUT_SetDebugName( g_pcbConstants, "CB_CONSTANTS" ); // Load the scene mesh WCHAR str[256]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, 256, L"ColumnScene\\scene.sdkmesh" ) ); g_SceneMesh.Create( pd3dDevice, str, false ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, 256, L"ColumnScene\\poles.sdkmesh" ) ); g_Poles.Create( pd3dDevice, str, false ); // Setup the camera D3DXVECTOR3 vecEye( 0.95f, 5.83f, -14.48f ); D3DXVECTOR3 vecAt ( 0.90f, 5.44f, -13.56f ); g_Camera.SetViewParams( &vecEye, &vecAt ); D3DXVECTOR3 vecEyeL = D3DXVECTOR3( 0,0,0 ); D3DXVECTOR3 vecAtL ( 0, -0.5, 1 ); g_LCamera.SetViewParams( &vecEyeL, &vecAtL ); // Create sampler states for point and linear // PointCmp D3D11_SAMPLER_DESC SamDesc; SamDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER; SamDesc.MipLODBias = 0.0f; SamDesc.MaxAnisotropy = 1; SamDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL; SamDesc.BorderColor[0] = SamDesc.BorderColor[1] = SamDesc.BorderColor[2] = SamDesc.BorderColor[3] = 1.0; SamDesc.MinLOD = 0; SamDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &g_pSamplePointCmp ) ); DXUT_SetDebugName( g_pSamplePointCmp, "PointCmp" ); // Point SamDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; SamDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &g_pSamplePoint ) ); DXUT_SetDebugName( g_pSamplePoint, "Point" ); // Linear SamDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &g_pSampleLinear ) ); DXUT_SetDebugName( g_pSampleLinear, "Linear" ); // Create a blend state to disable alpha blending D3D11_BLEND_DESC BlendState; ZeroMemory(&BlendState, sizeof(D3D11_BLEND_DESC)); BlendState.IndependentBlendEnable = FALSE; BlendState.RenderTarget[0].BlendEnable = FALSE; BlendState.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; hr = pd3dDevice->CreateBlendState(&BlendState, &g_pBlendStateNoBlend); DXUT_SetDebugName( g_pBlendStateNoBlend, "No Blend" ); BlendState.RenderTarget[0].RenderTargetWriteMask = 0; hr = pd3dDevice->CreateBlendState(&BlendState, &g_pBlendStateColorWritesOff); DXUT_SetDebugName( g_pBlendStateColorWritesOff, "Color Writes Off"); return S_OK; }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D11DestroyDevice(); g_D3DSettingsDlg.OnD3D11DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_DELETE( g_pTxtHelper ); g_Mesh11.Destroy(); SAFE_RELEASE(g_pGeometryShader); SAFE_RELEASE( g_pVertexLayout11 ); SAFE_RELEASE( g_pVertexBuffer ); SAFE_RELEASE( g_pVertexShader ); SAFE_RELEASE( g_pPixelShader ); SAFE_RELEASE( g_pSamLinear ); SAFE_RELEASE( g_pcbVSPerObject ); SAFE_RELEASE( g_pcbPSPerObject ); SAFE_RELEASE( g_pcbPSPerFrame ); SAFE_RELEASE( g_pRasterizerState ); SAFE_RELEASE( g_pRasterizerStateWF ); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].destroy(); } my_capsule.destroy(); // Shouldn't need to delete this as it's just a soft body and will be deleted later by the collision object cleanup. //for( int flagIndex = 0; flagIndex < m_flags.size(); ++flagIndex ) //{ //delete m_flags[flagIndex]; //} //cleanup in the reverse order of creation/initialization if( g_defaultSolver ) delete g_defaultSolver; if( g_cpuSolver ) delete g_cpuSolver; if( g_dx11Solver ) delete g_dx11Solver; if( g_dx11SIMDSolver ) delete g_dx11SIMDSolver; if( g_softBodyOutput ) delete g_softBodyOutput; for(int i=0; i< m_collisionShapes.size(); i++) delete m_collisionShapes[i]; //remove the rigidbodies from the dynamics world and delete them int i; for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) { btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; btRigidBody* body = btRigidBody::upcast(obj); if (body && body->getMotionState()) { delete body->getMotionState(); } m_dynamicsWorld->removeCollisionObject( obj ); delete obj; } delete m_dynamicsWorld; delete m_solver; delete m_broadphase; delete m_dispatcher; delete m_collisionConfiguration; }
void App::Render(ID3D11DeviceContext* d3dDeviceContext, ID3D11RenderTargetView* backBuffer, CDXUTSDKMesh& mesh_opaque, CDXUTSDKMesh& mesh_alpha, ID3D11ShaderResourceView* skybox, const D3DXMATRIXA16& worldMatrix, const CFirstPersonCamera* viewerCamera, const D3D11_VIEWPORT* viewport, const UIConstants* ui) { D3DXMATRIXA16 cameraProj = *viewerCamera->GetProjMatrix(); D3DXMATRIXA16 cameraView = *viewerCamera->GetViewMatrix(); D3DXMATRIXA16 cameraViewInv; D3DXMatrixInverse(&cameraViewInv, 0, &cameraView); // Compute composite matrices D3DXMATRIXA16 cameraViewProj = cameraView * cameraProj; D3DXMATRIXA16 cameraWorldViewProj = worldMatrix * cameraViewProj; // Fill in frame constants { D3D11_MAPPED_SUBRESOURCE mappedResource; d3dDeviceContext->Map(mPerFrameConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); PerFrameConstants* constants = static_cast<PerFrameConstants *>(mappedResource.pData); constants->mCameraWorldViewProj = cameraWorldViewProj; constants->mCameraWorldView = worldMatrix * cameraView; constants->mCameraViewProj = cameraViewProj; constants->mCameraProj = cameraProj; // NOTE: Complementary Z => swap near/far back constants->mCameraNearFar = D3DXVECTOR4(viewerCamera->GetFarClip(), viewerCamera->GetNearClip(), 0.0f, 0.0f); constants->mFramebufferDimensionsX = mGBufferWidth; constants->mFramebufferDimensionsY = mGBufferHeight; constants->mFramebufferDimensionsZ = 0; // Unused constants->mFramebufferDimensionsW = 0; // Unused constants->mUI = *ui; d3dDeviceContext->Unmap(mPerFrameConstants, 0); } // Geometry phase if (mesh_opaque.IsLoaded()) { mesh_opaque.ComputeInFrustumFlags(cameraWorldViewProj); } if (mesh_alpha.IsLoaded()) { mesh_alpha.ComputeInFrustumFlags(cameraWorldViewProj); } // Setup lights ID3D11ShaderResourceView *lightBufferSRV = SetupLights(d3dDeviceContext, cameraView); // Forward rendering takes a different path here if (ui->lightCullTechnique == CULL_FORWARD_NONE) { RenderForward(d3dDeviceContext, mesh_opaque, mesh_alpha, lightBufferSRV, viewerCamera, viewport, ui, false); } else if (ui->lightCullTechnique == CULL_FORWARD_PREZ_NONE) { RenderForward(d3dDeviceContext, mesh_opaque, mesh_alpha, lightBufferSRV, viewerCamera, viewport, ui, true); } else { RenderGBuffer(d3dDeviceContext, mesh_opaque, mesh_alpha, viewerCamera, viewport, ui); ComputeLighting(d3dDeviceContext, lightBufferSRV, viewport, ui); } // Render skybox and tonemap RenderSkyboxAndToneMap(d3dDeviceContext, backBuffer, skybox, mDepthBuffer->GetShaderResource(), viewport, ui); }
//-------------------------------------------------------------------------------------- // Create any D3D resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); XMFLOAT3 vCenter( 0.25767413f, -28.503521f, 111.00689f ); FLOAT fObjectRadius = 378.15607f; g_mCenterMesh = XMMatrixTranslation( -vCenter.x, -vCenter.y, -vCenter.z ); XMMATRIX m; m = XMMatrixRotationY( XM_PI ); g_mCenterMesh *= m; m = XMMatrixRotationX( XM_PI / 2.0f ); g_mCenterMesh *= m; // Init the UI widget for directional lighting V_RETURN( CDXUTDirectionWidget::StaticOnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); g_LightControl.SetRadius( fObjectRadius ); // Compile and create the effect. DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _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; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"DynamicShaderLinkageFX11.fx" ) ); ID3DBlob* pErrorBlob = nullptr; hr = D3DX11CompileEffectFromFile( str, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, D3DCOMPILE_EFFECT_ALLOW_SLOW_OPS, pd3dDevice, &g_pEffect, &pErrorBlob ); if ( pErrorBlob ) { OutputDebugStringA( reinterpret_cast<const char*>( pErrorBlob->GetBufferPointer() ) ); pErrorBlob->Release(); } if( FAILED(hr) ) { return hr; } #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"DynamicShaderLinkageFX11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, D3DCOMPILE_EFFECT_ALLOW_SLOW_OPS, &pEffectBuffer ) ); hr = D3DX11CreateEffectFromMemory( pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &g_pEffect ); SAFE_RELEASE( pEffectBuffer ); if ( FAILED(hr) ) return hr; #endif // Get the light Class Interfaces for setting values // and as potential binding sources. g_pAmbientLightClass = g_pEffect->GetVariableByName( "g_ambientLight" )->AsClassInstance(); g_pAmbientLightColor = g_pAmbientLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pAmbientLightEnable = g_pAmbientLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pHemiAmbientLightClass = g_pEffect->GetVariableByName( "g_hemiAmbientLight" )->AsClassInstance(); g_pHemiAmbientLightColor = g_pHemiAmbientLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pHemiAmbientLightEnable = g_pHemiAmbientLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pHemiAmbientLightGroundColor = g_pHemiAmbientLightClass->GetMemberByName( "m_vGroundColor" )->AsVector(); g_pHemiAmbientLightDirUp = g_pHemiAmbientLightClass->GetMemberByName( "m_vDirUp" )->AsVector(); g_pDirectionalLightClass = g_pEffect->GetVariableByName( "g_directionalLight")->AsClassInstance(); g_pDirectionalLightColor = g_pDirectionalLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pDirectionalLightEnable = g_pDirectionalLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pDirectionalLightDir = g_pDirectionalLightClass->GetMemberByName( "m_vLightDir" )->AsVector(); g_pEnvironmentLightClass = g_pEffect->GetVariableByName( "g_environmentLight")->AsClassInstance(); g_pEnvironmentLightColor = g_pEnvironmentLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pEnvironmentLightEnable = g_pEnvironmentLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pEyeDir = g_pEffect->GetVariableByName( "g_vEyeDir" )->AsVector(); // Acquire the material Class Instances for all possible material settings for( UINT i=0; i < MATERIAL_TYPE_COUNT; i++) { char pTechName[50]; sprintf_s( pTechName, sizeof(pTechName), "FeatureLevel11_%s", g_pMaterialClassNames[ i ] ); g_MaterialClasses[i].pTechnique = g_pEffect->GetTechniqueByName( pTechName ); g_MaterialClasses[i].pClass = g_pEffect->GetVariableByName( g_pMaterialClassNames[i] )->AsClassInstance(); g_MaterialClasses[i].pColor = g_MaterialClasses[i].pClass->GetMemberByName( "m_vColor" )->AsVector(); g_MaterialClasses[i].pSpecPower = g_MaterialClasses[i].pClass->GetMemberByName( "m_iSpecPower" )->AsScalar(); } // Select which technique to use based on the feature level we acquired D3D_FEATURE_LEVEL supportedFeatureLevel = DXUTGetD3D11DeviceFeatureLevel(); if (supportedFeatureLevel >= D3D_FEATURE_LEVEL_11_0) { // We are going to use Dynamic shader linkage with SM5 so we need to look up interface and class instance variables // Get the abstract class interfaces so we can dynamically permute and assign linkages g_pAmbientLightIface = g_pEffect->GetVariableByName( "g_abstractAmbientLighting" )->AsInterface(); g_pDirectionalLightIface = g_pEffect->GetVariableByName( "g_abstractDirectLighting" )->AsInterface(); g_pEnvironmentLightIface = g_pEffect->GetVariableByName( "g_abstractEnvironmentLighting" )->AsInterface(); g_pMaterialIface = g_pEffect->GetVariableByName( "g_abstractMaterial" )->AsInterface(); g_pTechnique = g_pEffect->GetTechniqueByName( "FeatureLevel11" ); } else // Lower feature levels than 11 have no support for Dynamic Shader Linkage - need to use a statically specialized shaders { LPCSTR pTechniqueName; g_pAmbientLightIface = nullptr; g_pDirectionalLightIface = nullptr; g_pEnvironmentLightIface = nullptr; g_pMaterialIface = nullptr; switch( supportedFeatureLevel ) { case D3D_FEATURE_LEVEL_10_1: pTechniqueName = "FeatureLevel10_1"; break; case D3D_FEATURE_LEVEL_10_0: pTechniqueName = "FeatureLevel10"; break; default: return E_FAIL; } g_pTechnique = g_pEffect->GetTechniqueByName( pTechniqueName ); } // Create our vertex input layout const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R16G16_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "BINORMAL", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; D3DX11_PASS_SHADER_DESC VsPassDesc; D3DX11_EFFECT_SHADER_DESC VsDesc; V_RETURN( g_pTechnique->GetPassByIndex(0)->GetVertexShaderDesc(&VsPassDesc) ); V_RETURN( VsPassDesc.pShaderVariable->GetShaderDesc(VsPassDesc.ShaderIndex, &VsDesc) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), VsDesc.pBytecode, VsDesc.BytecodeLength, &g_pVertexLayout11 ) ); DXUT_SetDebugName( g_pVertexLayout11, "Primary" ); // Load the mesh V_RETURN( g_Mesh11.Create( pd3dDevice, L"Squid\\squid.sdkmesh", false ) ); g_pWorldViewProjection = g_pEffect->GetVariableByName( "g_mWorldViewProjection" )->AsMatrix(); g_pWorld = g_pEffect->GetVariableByName( "g_mWorld" )->AsMatrix(); // Load a HDR Environment for reflections V_RETURN( DXUTCreateShaderResourceViewFromFile( pd3dDevice, L"Light Probes\\uffizi_cross.dds", &g_pEnvironmentMapSRV )); g_pEnvironmentMapVar = g_pEffect->GetVariableByName( "g_txEnvironmentMap" )->AsShaderResource(); g_pEnvironmentMapVar->SetResource( g_pEnvironmentMapSRV ); // Setup the camera's view parameters static const XMVECTORF32 s_vecEye = { 0.0f, 0.0f, -50.0f, 0.f }; g_Camera.SetViewParams( s_vecEye, g_XMZero ); g_Camera.SetRadius( fObjectRadius , fObjectRadius , fObjectRadius ); // Find Rasterizer State Object index for WireFrame / Solid rendering g_pFillMode = g_pEffect->GetVariableByName( "g_fillMode" )->AsScalar(); return hr; }
//-------------------------------------------------------------------------------------- // 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 = S_OK; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _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; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Tutorial11.fx" ) ); V_RETURN( D3DX11CompileEffectFromFile( str, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, 0, pd3dDevice, &g_pEffect, nullptr) ); #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"Tutorial11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, 0, &pEffectBuffer ) ); hr = D3DX11CreateEffectFromMemory( pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &g_pEffect ); SAFE_RELEASE( pEffectBuffer ); if ( FAILED(hr) ) return hr; #endif // Obtain the technique g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); // Obtain the variables g_ptxDiffuseVariable = g_pEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pWavinessVariable = g_pEffect->GetVariableByName( "Waviness" )->AsScalar(); g_pTimeVariable = g_pEffect->GetVariableByName( "Time" )->AsScalar(); // Set Waviness g_pWavinessVariable->SetFloat( g_fModelWaviness ); // Define the input layout 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 }, }; UINT numElements = ARRAYSIZE( layout ); // Create the input layout D3DX11_PASS_DESC PassDesc; V_RETURN( g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Set the input layout pd3dImmediateContext->IASetInputLayout( g_pVertexLayout ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiny\\tiny.sdkmesh" ) ); // Initialize the world matrices g_World = XMMatrixIdentity(); // Setup the camera's view parameters static const XMVECTORF32 s_Eye = { 0.0f, 3.0f, -800.0f, 0.f }; static const XMVECTORF32 s_At = { 0.0f, 1.0f, 0.0f, 0.f }; g_Camera.SetViewParams( s_Eye, s_At ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite ) ); g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont, g_pSprite, 15 ); DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_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 |= D3D10_SHADER_DEBUG; #endif // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Tutorial13.fx" ) ); V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL ) ); // Obtain the technique g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); // Obtain the variables g_ptxDiffuseVariable = g_pEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pExplodeVariable = g_pEffect->GetVariableByName( "Explode" )->AsScalar(); // Set Waviness g_pExplodeVariable->SetFloat( g_fExplode ); // Define the input layout const D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Set the input layout pd3dDevice->IASetInputLayout( g_pVertexLayout ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiny\\tiny.sdkmesh", true ) ); // Initialize the world matrices D3DXMatrixIdentity( &g_World ); // Initialize the camera D3DXVECTOR3 Eye( 0.0f, 0.0f, -800.0f ); D3DXVECTOR3 At( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &Eye, &At ); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, 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_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } // // Clear the back buffer // float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red, green, blue, alpha ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); // // Clear the depth stencil // ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // // Update variables that change once per frame // g_pProjectionVariable->SetMatrix( ( float* )g_Camera.GetProjMatrix() ); g_pViewVariable->SetMatrix( ( float* )g_Camera.GetViewMatrix() ); g_pWorldVariable->SetMatrix( ( float* )&g_World ); // // Set the Vertex Layout // pd3dDevice->IASetInputLayout( g_pVertexLayout ); // // Render the mesh // UINT Strides[1]; UINT Offsets[1]; ID3D10Buffer* pVB[1]; pVB[0] = g_Mesh.GetVB10( 0, 0 ); Strides[0] = ( UINT )g_Mesh.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dDevice->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dDevice->IASetIndexBuffer( g_Mesh.GetIB10( 0 ), g_Mesh.GetIBFormat10( 0 ), 0 ); D3D10_TECHNIQUE_DESC techDesc; g_pTechnique->GetDesc( &techDesc ); SDKMESH_SUBSET* pSubset = NULL; ID3D10ShaderResourceView* pDiffuseRV = NULL; D3D10_PRIMITIVE_TOPOLOGY PrimType; for( UINT p = 0; p < techDesc.Passes; ++p ) { for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); ++subset ) { pSubset = g_Mesh.GetSubset( 0, subset ); PrimType = g_Mesh.GetPrimitiveType10( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType ); pd3dDevice->IASetPrimitiveTopology( PrimType ); pDiffuseRV = g_Mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV10; g_ptxDiffuseVariable->SetResource( pDiffuseRV ); g_pTechnique->GetPassByIndex( p )->Apply( 0 ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart ); } } //the mesh class also had a render method that allows rendering the mesh with the most common options //g_Mesh.Render( pd3dDevice, g_pTechnique, g_ptxDiffuseVariable ); // // Render the UI // g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); }
//-------------------------------------------------------------------------------------- // 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; } // // Clear the back buffer // auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue ); // // Clear the depth stencil // auto pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); XMMATRIX mView = g_Camera.GetViewMatrix(); XMMATRIX mProj = g_Camera.GetProjMatrix(); XMMATRIX mWorldViewProjection = g_World * mView * mProj; // // Update variables that change once per frame // g_pProjectionVariable->SetMatrix( ( float* )&mProj ); g_pViewVariable->SetMatrix( ( float* )&mView ); g_pWorldVariable->SetMatrix( ( float* )&g_World ); g_pTimeVariable->SetFloat( ( float )fTime ); // // Set the Vertex Layout // pd3dImmediateContext->IASetInputLayout( g_pVertexLayout ); // // Render the mesh // UINT Strides[1]; UINT Offsets[1]; ID3D11Buffer* pVB[1]; pVB[0] = g_Mesh.GetVB11( 0, 0 ); Strides[0] = ( UINT )g_Mesh.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dImmediateContext->IASetIndexBuffer( g_Mesh.GetIB11( 0 ), g_Mesh.GetIBFormat11( 0 ), 0 ); D3DX11_TECHNIQUE_DESC techDesc; HRESULT hr; V( g_pTechnique->GetDesc( &techDesc ) ); for( UINT p = 0; p < techDesc.Passes; ++p ) { for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); ++subset ) { auto pSubset = g_Mesh.GetSubset( 0, subset ); auto PrimType = g_Mesh.GetPrimitiveType11( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType ); pd3dImmediateContext->IASetPrimitiveTopology( PrimType ); auto pDiffuseRV = g_Mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV11; g_ptxDiffuseVariable->SetResource( pDiffuseRV ); g_pTechnique->GetPassByIndex( p )->Apply( 0, pd3dImmediateContext ); pd3dImmediateContext->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart ); } } // // Render the UI // g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10 ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite10 ) ); g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont10, g_pSprite10, 15 ); V_RETURN( CDXUTDirectionWidget::StaticOnD3D10CreateDevice( pd3dDevice ) ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"MotionBlur10.fx" ) ); DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_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 |= D3D10_SHADER_DEBUG; #endif V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL ) ); // Obtain the technique handles g_pRenderScene = g_pEffect->GetTechniqueByName( "RenderScene" ); g_pRenderSkinnedScene = g_pEffect->GetTechniqueByName( "RenderSkinnedScene" ); g_pRenderMotionBlur = g_pEffect->GetTechniqueByName( "RenderMotionBlur" ); g_pRenderSkinnedMotionBlur = g_pEffect->GetTechniqueByName( "RenderSkinnedMotionBlur" ); // Obtain the parameter handles g_pmWorldViewProj = g_pEffect->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmViewProj = g_pEffect->GetVariableByName( "g_mViewProj" )->AsMatrix(); g_pmWorldView = g_pEffect->GetVariableByName( "g_mWorldView" )->AsMatrix(); g_pmBlurViewProj = g_pEffect->GetVariableByName( "g_mBlurViewProj" )->AsMatrix(); g_pmBlurWorld = g_pEffect->GetVariableByName( "g_mBlurWorld" )->AsMatrix(); g_pmBoneWorld = g_pEffect->GetVariableByName( "g_mBoneWorld" )->AsMatrix(); g_ptxDiffuse = g_pEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pfFrameTime = g_pEffect->GetVariableByName( "g_fFrameTime" )->AsScalar(); g_piNumSteps = g_pEffect->GetVariableByName( "g_iNumSteps" )->AsScalar(); g_pfFadeDist = g_pEffect->GetVariableByName( "g_fFadeDist" )->AsScalar(); // Define our vertex data layout const D3D10_INPUT_ELEMENT_DESC staticlayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; D3D10_PASS_DESC PassDesc; g_pRenderScene->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( staticlayout, 4, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pStaticVertexLayout ) ); //Create the scene meshes g_SceneMesh.Create( pd3dDevice, L"motionblur\\WindMillStage.sdkmesh" ); g_FanMesh.Create( pd3dDevice, L"motionblur\\Fan.sdkmesh" ); D3DXMatrixTranslation( &g_mFanWorld, 0.0f, 3.62f, 2.012f ); //Create the Linked Meshes g_pLinkedMeshes = new CDXUTSDKMesh[ g_NumLinkedMeshes ]; if( !g_pLinkedMeshes ) return E_OUTOFMEMORY; for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) { g_pLinkedMeshes[iMesh].Create( pd3dDevice, g_MeshLinkages[iMesh].szMeshName ); } const D3D10_INPUT_ELEMENT_DESC skinnedlayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "BONES", 0, DXGI_FORMAT_R32G32B32A32_UINT, 0, 44, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "WEIGHTS", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 60, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; g_pRenderSkinnedScene->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( skinnedlayout, 6, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pSkinnedVertexLayout ) ); g_AnimMesh.Create( pd3dDevice, L"motionblur\\Warrior.sdkmesh" ); g_AnimMesh.LoadAnimation( L"motionblur\\Warrior.sdkmesh_anim" ); //camera D3DXVECTOR3 vEye( 2.0f, 1.3f, -4.0f ); D3DXVECTOR3 vAt( 0.0f, 1.0f, -1.11f ); g_Camera.SetViewParams( &vEye, &vAt ); return S_OK; }