//-------------------------------------------------------------------------------------- // Name: BloomTexture() // Desc: Bloom the pSrcTexture and place the result in pDstTexture //-------------------------------------------------------------------------------------- VOID PostProcess::BloomTexture( LPDIRECT3DTEXTURE9 pSrcTexture, BOOL bBloomAcrossWidth, LPDIRECT3DTEXTURE9 pDstTexture, FLOAT fSize, FLOAT fBrightness ) { // Make sure that the required shaders and objects exist assert( m_pBloomPS ); assert( pSrcTexture && pDstTexture ); XGTEXTURE_DESC SrcDesc; XGGetTextureDesc( pSrcTexture, 0, &SrcDesc ); // Create and set a render target PushRenderTarget( 0L, CreateRenderTarget( pDstTexture ) ); XMVECTOR avSampleOffsets[MAX_SAMPLES]; XMVECTOR avSampleWeights[MAX_SAMPLES]; if( bBloomAcrossWidth ) GetSampleOffsets_Bloom( SrcDesc.Width, SrcDesc.Height, 0.0f * XM_PIDIV2, avSampleOffsets, avSampleWeights, fSize, fBrightness ); else GetSampleOffsets_Bloom( SrcDesc.Width, SrcDesc.Height, 1.0f * XM_PIDIV2, avSampleOffsets, avSampleWeights, fSize, fBrightness ); g_pd3dDevice->SetPixelShaderConstantF( PSCONST_avSampleOffsets, ( FLOAT* )avSampleOffsets, MAX_SAMPLES ); g_pd3dDevice->SetPixelShaderConstantF( PSCONST_avSampleWeights, ( FLOAT* )avSampleWeights, MAX_SAMPLES ); g_pd3dDevice->SetPixelShader( m_pBloomPS ); g_pd3dDevice->SetTexture( 0, pSrcTexture ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); // Draw a fullscreen quad to sample the RT DrawFullScreenQuad(); g_pd3dDevice->Resolve( D3DRESOLVE_RENDERTARGET0, NULL, pDstTexture, NULL, 0, 0, NULL, 1.0f, 0L, NULL ); // Cleanup and return PopRenderTarget( 0L )->Release(); g_pd3dDevice->SetPixelShader( NULL ); }
/* void HDRLight::_starSource_To_BloomSource() { D3DXVECTOR2 avSampleOffsets[MAX_SAMPLES]; // Get the new render target surface PDIRECT3DSURFACE9 pSurfBloomSource; g_pTexBloomSource->GetSurfaceLevel( 0, &pSurfBloomSource ); // Get the rectangle describing the sampled portion of the source texture. // Decrease the rectangle to adjust for the single pixel black border. RECT rectSrc; _getTextureRect( g_pTexStarSource, &rectSrc ); InflateRect( &rectSrc, -1, -1 ); // Get the destination rectangle. // Decrease the rectangle to adjust for the single pixel black border. RECT rectDest; _getTextureRect( g_pTexBloomSource, &rectDest ); InflateRect( &rectDest, -1, -1 ); // Get the correct texture coordinates to apply to the rendered quad in order // to sample from the source rectangle and render into the destination rectangle CoordRect coords; _getTextureCoords( g_pTexStarSource, &rectSrc, g_pTexBloomSource, &rectDest, &coords ); // Get the sample offsets used within the pixel shader D3DSURFACE_DESC desc; g_pTexBrightPass->GetLevelDesc( 0, &desc ); GetSampleOffsets_DownScale2x2( desc.Width, desc.Height, avSampleOffsets ); g_HDREffect->SetValue( "g_avSampleOffsets", avSampleOffsets, sizeof( avSampleOffsets ) ); // Create an exact 1/2 x 1/2 copy of the source texture g_HDREffect->SetTechnique( "DownScale2x2" ); g_pd3dDevice->SetRenderTarget( 0, pSurfBloomSource ); g_pd3dDevice->SetTexture( 0, g_pTexStarSource ); g_pd3dDevice->SetScissorRect( &rectDest ); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, TRUE ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); UINT uiPassCount, uiPass; g_HDREffect->Begin( &uiPassCount, 0 ); for( uiPass = 0; uiPass < uiPassCount; uiPass++ ) { g_HDREffect->BeginPass( uiPass ); // Draw a fullscreen quad _drawFullScreenQuad( coords.fLeftU,coords.fTopV,coords.fRightU,coords.fBottomV ); g_HDREffect->EndPass(); } g_HDREffect->End(); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, FALSE ); SAFE_RELEASE( pSurfBloomSource ); }*/ void HDRLight::_renderBloom() { UINT uiPassCount, uiPass; int i = 0; D3DXVECTOR2 avSampleOffsets[MAX_SAMPLES]; FLOAT afSampleOffsets[MAX_SAMPLES]; D3DXVECTOR4 avSampleWeights[MAX_SAMPLES]; PDIRECT3DSURFACE9 pSurfScaledHDR; g_pTexSceneScaled->GetSurfaceLevel( 0, &pSurfScaledHDR ); PDIRECT3DSURFACE9 pSurfBloom; g_apTexBloom[0]->GetSurfaceLevel( 0, &pSurfBloom ); PDIRECT3DSURFACE9 pSurfHDR; g_pTexScene->GetSurfaceLevel( 0, &pSurfHDR ); PDIRECT3DSURFACE9 pSurfTempBloom; g_apTexBloom[1]->GetSurfaceLevel( 0, &pSurfTempBloom ); PDIRECT3DSURFACE9 pSurfBloomSource; g_apTexBloom[2]->GetSurfaceLevel( 0, &pSurfBloomSource ); // Clear the bloom texture g_pd3dDevice->ColorFill( pSurfBloom, NULL, D3DCOLOR_ARGB( 0, 0, 0, 0 ) ); RECT rectSrc; GetTextureRect( g_pTexBloomSource, &rectSrc ); InflateRect( &rectSrc, -1, -1 ); RECT rectDest; GetTextureRect( g_apTexBloom[2], &rectDest ); InflateRect( &rectDest, -1, -1 ); CoordRect coords; GetTextureCoords( g_pTexBloomSource, &rectSrc, g_apTexBloom[2], &rectDest, &coords ); D3DSURFACE_DESC desc; g_pTexBloomSource->GetLevelDesc( 0, &desc ); g_HDREffect->SetTechnique( "GaussBlur5x5" ); GetSampleOffsets_GaussBlur5x5( desc.Width, desc.Height, avSampleOffsets, avSampleWeights, 1.0f ); g_HDREffect->SetValue( "g_avSampleOffsets", avSampleOffsets, sizeof( avSampleOffsets ) ); g_HDREffect->SetValue( "g_avSampleWeights", avSampleWeights, sizeof( avSampleWeights ) ); g_pd3dDevice->SetRenderTarget( 0, pSurfBloomSource ); g_pd3dDevice->SetTexture( 0, g_pTexBloomSource ); g_pd3dDevice->SetScissorRect( &rectDest ); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, TRUE ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); g_HDREffect->Begin( &uiPassCount, 0 ); for( uiPass = 0; uiPass < uiPassCount; uiPass++ ) { g_HDREffect->BeginPass( uiPass ); // Draw a fullscreen quad to sample the RT DrawFullScreenQuad(g_pd3dDevice, coords.fLeftU,coords.fTopV,coords.fRightU,coords.fBottomV ); g_HDREffect->EndPass(); } g_HDREffect->End(); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, FALSE ); g_apTexBloom[2]->GetLevelDesc( 0, &desc ); GetSampleOffsets_Bloom( desc.Width, afSampleOffsets, avSampleWeights, 3.0f, 2.0f ); for( i = 0; i < MAX_SAMPLES; i++ ) { avSampleOffsets[i] = D3DXVECTOR2( afSampleOffsets[i], 0.0f ); } g_HDREffect->SetTechnique( "Bloom" ); g_HDREffect->SetValue( "g_avSampleOffsets", avSampleOffsets, sizeof( avSampleOffsets ) ); g_HDREffect->SetValue( "g_avSampleWeights", avSampleWeights, sizeof( avSampleWeights ) ); g_pd3dDevice->SetRenderTarget( 0, pSurfTempBloom ); g_pd3dDevice->SetTexture( 0, g_apTexBloom[2] ); g_pd3dDevice->SetScissorRect( &rectDest ); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, TRUE ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); g_HDREffect->Begin( &uiPassCount, 0 ); for( uiPass = 0; uiPass < uiPassCount; uiPass++ ) { g_HDREffect->BeginPass( uiPass ); // Draw a fullscreen quad to sample the RT DrawFullScreenQuad(g_pd3dDevice, coords.fLeftU,coords.fTopV,coords.fRightU,coords.fBottomV ); g_HDREffect->EndPass(); } g_HDREffect->End(); g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, FALSE ); g_apTexBloom[1]->GetLevelDesc( 0, &desc ); GetSampleOffsets_Bloom( desc.Height, afSampleOffsets, avSampleWeights, 3.0f, 2.0f ); for( i = 0; i < MAX_SAMPLES; i++ ) { avSampleOffsets[i] = D3DXVECTOR2( 0.0f, afSampleOffsets[i] ); } GetTextureRect( g_apTexBloom[1], &rectSrc ); InflateRect( &rectSrc, -1, -1 ); GetTextureCoords( g_apTexBloom[1], &rectSrc, g_apTexBloom[0], NULL, &coords ); g_HDREffect->SetTechnique( "Bloom" ); g_HDREffect->SetValue( "g_avSampleOffsets", avSampleOffsets, sizeof( avSampleOffsets ) ); g_HDREffect->SetValue( "g_avSampleWeights", avSampleWeights, sizeof( avSampleWeights ) ); g_pd3dDevice->SetRenderTarget( 0, pSurfBloom ); g_pd3dDevice->SetTexture( 0, g_apTexBloom[1] ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT ); g_HDREffect->Begin( &uiPassCount, 0 ); for( uiPass = 0; uiPass < uiPassCount; uiPass++ ) { g_HDREffect->BeginPass( uiPass ); // Draw a fullscreen quad to sample the RT DrawFullScreenQuad(g_pd3dDevice, coords.fLeftU,coords.fTopV,coords.fRightU,coords.fBottomV ); g_HDREffect->EndPass(); } g_HDREffect->End(); SAFE_RELEASE( pSurfBloomSource ); SAFE_RELEASE( pSurfTempBloom ); SAFE_RELEASE( pSurfBloom ); SAFE_RELEASE( pSurfHDR ); SAFE_RELEASE( pSurfScaledHDR ); }