Esempio n. 1
0
void GaussianBlurRender::Render(Engine& engine, IDirect3DSurface9* backBuffer, IDirect3DSurface9* dsSurface)
{
	if (!_shader || !GetRT() || !GetRT()->IsInit() || !_colorTex)
		throw int();

	_shader->Apply(engine);
	IDirect3DSurface9* targetSurf;
	GetRT()->GetTex()->GetSurfaceLevel(0, &targetSurf);
	GetDriver().GetDevice()->SetRenderTarget(0, targetSurf);
	engine.SetTexture(0, _colorTex->GetTex());

	//Horizontal
	_shader->curTechnique = GaussianBlurShader::ttHorizontal;
	engine.BeginDraw();
	DrawScreenQuad(engine);
	engine.EndDraw();

	//Vertical
	_shader->curTechnique = GaussianBlurShader::ttVertical;
	engine.BeginDraw();
	DrawScreenQuad(engine);
	engine.EndDraw();

	_shader->UnApply(engine);
	engine.SetTexture(0, 0);	
}
Esempio n. 2
0
void dx103DFluidRenderer::ComputeEdgeTexture()
{
	CRenderTarget* pTarget = RImplementation.Target;
	pTarget->u_setrt(RT[RRT_RayDataTexSmall],0,0,0);		// LDR RT
	RCache.set_Element(m_RendererTechnique[RS_QuadDownSampleRayDataTexture]);

	// First setup viewport to match the size of the destination low-res texture
	//D3D10_VIEWPORT rtViewport;
	//rtViewport.TopLeftX = 0;
	//rtViewport.TopLeftY = 0;
	//rtViewport.MinDepth = 0;
	//rtViewport.MaxDepth = 1;
	//rtViewport.Width = renderTextureWidth;
	//rtViewport.Height = renderTextureHeight;
	//m_pD3DDevice->RSSetViewports(1,&rtViewport);
	RImplementation.rmNormal();
	//pRTWidthVar->SetFloat((float)renderTextureWidth);
	RCache.set_c(strRTWidth, (float)m_iRenderTextureWidth);
	//pRTHeightVar->SetFloat((float)renderTextureHeight);
	RCache.set_c(strRTHeight, (float)m_iRenderTextureHeight);

	// Downsample the rayDataTexture to a new small texture, simply using point sample (no filtering)
	//m_pD3DDevice->OMSetRenderTargets( 1, &pRayDataSmallRTV , NULL ); 
	//pRayDataVar->SetResource(pRayDataSRV);
	//pTechnique->GetPassByName("QuadDownSampleRayDataTexture")->Apply(0);
	DrawScreenQuad();

	// Create an edge texture, performing edge detection on 'rayDataTexSmall'
	pTarget->u_setrt(RT[RRT_EdgeTex],0,0,0);		// LDR RT
	RCache.set_Element(m_RendererTechnique[RS_QuadEdgeDetect]);
	//m_pD3DDevice->OMSetRenderTargets( 1, &pEdgeRTV , NULL ); 
	//pRayDataSmallVar->SetResource(pRayDataSmallSRV);
	//pTechnique->GetPassByName("QuadEdgeDetect")->Apply(0);
	DrawScreenQuad();
}
Esempio n. 3
0
void DrawImage(LPDIRECT3DTEXTURE9 pSource1, sImageInfo *pInfo, float x0, float y0, float width, float height)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	g_pEffect->SetTechnique(g_pAddImageShader);
	D3DXHANDLE pTexture1Var = g_pEffect->GetParameterByName(NULL, "Image");

	g_pEffect->SetTexture(pTexture1Var, pSource1);

	g_pEffect->Begin(NULL, 0);
	g_pEffect->BeginPass(0);

	DrawScreenQuad(pInfo, x0, y0, width, height);

	g_pEffect->EndPass();
	g_pEffect->End();
}
Esempio n. 4
0
void dx103DFluidRenderer::Draw(const dx103DFluidData &FluidData)
{
	//	We don't need ZB anyway
	RCache.set_ZB(0);

	const dx103DFluidData::Settings &VolumeSettings = FluidData.GetSettings();
	const bool bRenderFire = (VolumeSettings.m_SimulationType == dx103DFluidData::ST_FIRE);

	FogLighting  LightData;

	CalculateLighting(FluidData, LightData);


	const Fmatrix &transform = FluidData.GetTransform();

	RCache.set_xform_world( transform );

	//pColorTexVar->SetResource(pSourceTexSRV);

	//	Set shader element to set up all necessary constants to constant buffer
	//	If you change constant buffer layout make sure this hack works ok.
	RCache.set_Element(m_RendererTechnique[RS_CompRayData_Back]);

	// Set some variables required by the shaders:
	//=========================================================================

	// The near and far planes are used to unproject the scene's z-buffer values
	//pZNearVar->SetFloat(g_zNear);
	RCache.set_c(strZNear, VIEWPORT_NEAR);
	//pZFarVar->SetFloat(g_zFar);
	RCache.set_c(strZFar, g_pGamePersistent->Environment().CurrentEnv->far_plane);

	//D3DXMATRIX worldView = g_gridWorld * g_View;
	D3DXMATRIX gridWorld;
	//D3DXMatrixTranspose(&gridWorld, (D3DXMATRIX*)&transform);
	gridWorld = *(D3DXMATRIX*)&transform;
	D3DXMATRIX View;
	//D3DXMatrixTranspose(&View, (D3DXMATRIX*)&RCache.xforms.m_v);
	View = *(D3DXMATRIX*)&RCache.xforms.m_v;
	D3DXMATRIX WorldView = gridWorld * View;

	//	Modified later
	//Fmatrix	WorldView = RCache.xforms.m_wv;
	//RCache.set_xform_world( transform );

	// The length of one of the axis of the worldView matrix is the length of longest side of the box
	//  in view space. This is used to convert the length of a ray from view space to grid space.
	//D3DXVECTOR3 worldXaxis = D3DXVECTOR3(worldView._11, worldView._12, worldView._13);
	D3DXVECTOR3 worldXaxis = D3DXVECTOR3(WorldView._11, WorldView._12, WorldView._13);
	float worldScale = D3DXVec3Length(&worldXaxis);
	//pGridScaleFactorVar->SetFloat( worldScale );
	RCache.set_c(strGridScaleFactor, worldScale);

	// We prepend the current world matrix with this other matrix which adds an offset (-0.5, -0.5, -0.5)
	//  and scale factors to account for unequal number of voxels on different sides of the volume box. 
	// This is because we want to preserve the aspect ratio of the original simulation grid when 
	//  raytracing through it.
	//worldView = m_gridMatrix * worldView;
	WorldView = m_gridMatrix * WorldView;
	//WorldView.mulB_44(m_gridMatrix);

//	Fmatrix temp;
//	temp = transform;
//	temp.mulB_44(m_gridMatrix);

//	RCache.set_xform_world( temp );
//	return;
	

	// worldViewProjection is used to transform the volume box to screen space
	//D3DXMATRIX WorldViewProjection;
	D3DXMATRIX WorldViewProjection;
	//Fmatrix WorldViewProjection;
	//worldViewProjection = worldView * g_Projection;
	D3DXMATRIX Projection;
	//D3DXMatrixTranspose(&Projection, (D3DXMATRIX*)&RCache.xforms.m_p);
	Projection = *(D3DXMATRIX*)&RCache.xforms.m_p;
	WorldViewProjection = WorldView * Projection;
	//WorldViewProjection.mul(RCache.xforms.m_p, WorldView);
	//pWorldViewProjectionVar->SetMatrix( (float*)&worldViewProjection );
	//	NVidia used different matrix orientation
	//WorldViewProjection.transpose();
	//RCache.set_c(strWorldViewProjection, WorldViewProjection);
	RCache.set_c(strWorldViewProjection, *(Fmatrix*)&WorldViewProjection);
	//WorldViewProjection.transpose();

	// invWorldViewProjection is used to transform positions in the "near" plane into grid space
	//D3DXMATRIX invWorldViewProjection;
	D3DXMATRIX InvWorldViewProjection;
	//Fmatrix InvWorldViewProjection;
	//WorldViewProjection.transpose();
	D3DXMatrixInverse((D3DXMATRIX*)&InvWorldViewProjection, NULL, (D3DXMATRIX*)&WorldViewProjection);
	//WorldViewProjection.transpose();
	//pInvWorldViewProjectionVar->SetMatrix((float*)&invWorldViewProjection);
	//InvWorldViewProjection.transpose();
	//RCache.set_c(strInvWorldViewProjection, InvWorldViewProjection);
	RCache.set_c(strInvWorldViewProjection, *(Fmatrix*)&InvWorldViewProjection);
	//InvWorldViewProjection.transpose();

	// Compute the inverse of the worldView matrix 
	//D3DXMATRIX worldViewInv;
	D3DXMATRIX WorldViewInv;
	//Fmatrix WorldViewInv;
	D3DXMatrixInverse((D3DXMATRIX*)&WorldViewInv, NULL, (D3DXMATRIX*)&WorldView);
	// Compute the eye's position in "grid space" (the 0-1 texture coordinate cube)
	//D3DXVECTOR4 eyeInGridSpace;
	//D3DXVECTOR3 origin(0,0,0);
	D3DXVECTOR4 EyeInGridSpace;
	D3DXVECTOR3 Origin(0,0,0);
	//Fvector4 EyeInGridSpace;
	//Fvector3 Origin = Fvector3().set(0,0,0);
	//WorldViewInv.transpose();
	D3DXVec3Transform((D3DXVECTOR4*)&EyeInGridSpace, (D3DXVECTOR3*)&Origin, (D3DXMATRIX*)&WorldViewInv);
	//WorldViewInv.transpose();
	//pEyeOnGridVar->SetFloatVector((float*)&eyeInGridSpace);
	RCache.set_c(strEyeOnGrid, *(Fvector4*)&EyeInGridSpace);

	float color[4] = {0, 0, 0, 0 };


	// Ray cast and render to a temporary buffer
	//=========================================================================

	// Partial init of viewport struct used below
	//D3D10_VIEWPORT rtViewport;
	//rtViewport.TopLeftX = 0;
	//rtViewport.TopLeftY = 0;
	//rtViewport.MinDepth = 0;
	//rtViewport.MaxDepth = 1;


	// Compute the ray data required by the raycasting pass below.
	//  This function will render to a buffer of float4 vectors, where
	//  xyz is starting position of the ray in grid space
	//  w is the length of the ray in view space
	ComputeRayData();


	// Do edge detection on this image to find any 
	//  problematic areas where we need to raycast at higher resolution
	ComputeEdgeTexture();


	// Raycast into the temporary render target: 
	//  raycasting is done at the smaller resolution, using a fullscreen quad
	//m_pD3DDevice->ClearRenderTargetView( pRayCastRTV, color );
	HW.pDevice->ClearRenderTargetView( RT[RRT_RayCastTex]->pRT, color );
	//m_pD3DDevice->OMSetRenderTargets( 1, &pRayCastRTV , NULL ); 
	CRenderTarget* pTarget = RImplementation.Target;
	pTarget->u_setrt(RT[RRT_RayCastTex],0,0,0);		// LDR RT

	//rtViewport.Width = renderTextureWidth;
	//rtViewport.Height = renderTextureHeight;
	//m_pD3DDevice->RSSetViewports(1,&rtViewport);
	RImplementation.rmNormal();

	//pTechnique->GetPassByName("QuadRaycast")->Apply(0);
	if (bRenderFire)
		RCache.set_Element(m_RendererTechnique[RS_QuadRaycastFire]);
	else
		RCache.set_Element(m_RendererTechnique[RS_QuadRaycastFog]);

	//pRTWidthVar->SetFloat((float)renderTextureWidth);
	RCache.set_c(strRTWidth, (float)m_iRenderTextureWidth);
	//pRTHeightVar->SetFloat((float)renderTextureHeight);
	RCache.set_c(strRTHeight, (float)m_iRenderTextureHeight);

	//pRayDataSmallVar->SetResource(pRayDataSmallSRV);

	DrawScreenQuad();


	// Render to the back buffer sampling from the raycast texture that we just created
	//  If and edge was detected at the current pixel we will raycast again to avoid
	//  smoke aliasing artifacts at scene edges
	//ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();
	//ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView();
	//m_pD3DDevice->OMSetRenderTargets( 1, &pRTV , pDSV ); 
	//	Restore render state
	if( !RImplementation.o.dx10_msaa )
		pTarget->u_setrt( pTarget->rt_Generic_0,0,0,HW.pBaseZB);		// LDR RT
	else
		pTarget->u_setrt( pTarget->rt_Generic_0,0,0,pTarget->rt_MSAADepth->pZRT);		// LDR RT

	if (bRenderFire)
		RCache.set_Element(m_RendererTechnique[RS_QuadRaycastCopyFire]);
	else
		RCache.set_Element(m_RendererTechnique[RS_QuadRaycastCopyFog]);

	//rtViewport.Width = g_Width;
	//rtViewport.Height = g_Height;
	//m_pD3DDevice->RSSetViewports(1,&rtViewport);
	RImplementation.rmNormal();

	//pRTWidthVar->SetFloat((float)g_Width);
	RCache.set_c(strRTWidth, (float)Device.dwWidth);
	//pRTHeightVar->SetFloat((float)g_Height);
	RCache.set_c(strRTHeight, (float)Device.dwHeight);

	RCache.set_c(strDiffuseLight, LightData.m_vLightIntencity.x, LightData.m_vLightIntencity.y, LightData.m_vLightIntencity.z, 1.0f);

	//pRayCastVar->SetResource(pRayCastSRV);
	//pEdgeVar->SetResource(pEdgeSRV);

	//pTechnique->GetPassByName("QuadRaycastCopy")->Apply(0);
	DrawScreenQuad();
}
void ForwardRenderer::DrawScene()
{
    DrawShadowMap();

    RendererCore::Instance()->SetOriginalRenderTargetDepth();

    float blendFactor[] = { 0.0f, 0.0f, 0.0f, 0.0f };

    // Set constants
    XMMATRIX view = XMLoadFloat4x4( RendererCore::Instance()->GetView() );
    XMMATRIX proj = XMLoadFloat4x4( RendererCore::Instance()->GetProj() );

    XMMATRIX shadowTransform = XMLoadFloat4x4( &m_ShadowTransform );


    int iCount = m_vDrawElements.size();
    for ( int i = 0; i < iCount; ++i )
    {
        DrawElement* pElem = m_vDrawElements[i];
        if ( !pElem )
            continue;

        UINT stride = pElem->stride;
        UINT offset = pElem->offset;

        GetD3D11DeviceImmContext()->IASetInputLayout( pElem->m_pInputLayout );
        GetD3D11DeviceImmContext()->IASetPrimitiveTopology( pElem->ePrimitiveTopology );

        pElem->m_spShader->SetDirLight( m_DirLights[0] );
        pElem->m_spShader->SetDirLights( m_DirLights );
        pElem->m_spShader->SetPointLight( m_PointLight );
        pElem->m_spShader->SetSpotLight( m_SpotLight );
        pElem->m_spShader->SetEyePosW( RendererCore::Instance()->GetEyePosW() );
        pElem->m_spShader->SetFogStart( 15.0f );
        pElem->m_spShader->SetFogRange( 300.0f );
        pElem->m_spShader->SetFogColor( Colors::Silver );

        for ( auto itor = pElem->m_vecSubElement.begin(); itor != pElem->m_vecSubElement.end() ; ++itor )
        {
            ID3DX11EffectTechnique* pTech = pElem->m_spShader->GetTech( (*itor).m_iTechIndex );
            if ( !pTech )
                continue;


            ID3D11Buffer* pVB = pElem->m_spVB->GetVB();
            GetD3D11DeviceImmContext()->IASetVertexBuffers( 0, 1, &pVB, &stride, &offset );

            if ( pElem->m_spIB )
                GetD3D11DeviceImmContext()->IASetIndexBuffer( pElem->m_spIB->GetIB(), DXGI_FORMAT_R32_UINT, 0 );

            if ( pElem->m_pRasterS )
                GetD3D11DeviceImmContext()->RSSetState( pElem->m_pRasterS );
            if ( pElem->m_pBlendS )
                GetD3D11DeviceImmContext()->OMSetBlendState( pElem->m_pBlendS, blendFactor, 0xffffffff );
            if ( pElem->m_pDepthStencilS )
                GetD3D11DeviceImmContext()->OMSetDepthStencilState( pElem->m_pDepthStencilS, pElem->m_uiStencilRef );

            XMMATRIX world;
            XMFLOAT3 oldLightDirections;
            if ( pElem->m_bStencilReflect )
            {
                XMVECTOR mirrorPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); // xy plane
                XMMATRIX R = XMMatrixReflect( mirrorPlane );
                world = XMLoadFloat4x4( &pElem->m_vecSubElement[0].m_World ) * R;

                oldLightDirections = m_DirLights[0].Direction;

                XMVECTOR lightDir = XMLoadFloat3( &m_DirLights[0].Direction );
                XMVECTOR reflectedLightDir = XMVector3TransformNormal( lightDir, R );
                XMStoreFloat3( &m_DirLights[0].Direction, reflectedLightDir );

                //ÀӽùæÆí
                pElem->m_spShader->SetDirLight( m_DirLights[0] );
            }
            else if ( pElem->m_bShadowmap )
            {
                XMVECTOR shadowPlane = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); // xz plane
                XMVECTOR toMainLight = -XMLoadFloat3( &m_DirLights[0].Direction );
                XMMATRIX S = XMMatrixShadow( shadowPlane, toMainLight );
                XMMATRIX shadowOffsetY = XMMatrixTranslation( 0.0f, 0.001f, 0.0f );

                world = XMLoadFloat4x4( &pElem->m_vecSubElement[0].m_World )*S*shadowOffsetY;
            }
            else
            {
                world = XMLoadFloat4x4( &pElem->m_vecSubElement[0].m_World );
            }

            TexturePtr spDiffuseMap = (*itor).m_spDiffuseMap;
            TexturePtr spCubeMap = (*itor).m_spCubeMap;
            TexturePtr spNormalMap = (*itor).m_spNormalMap;
            TexturePtr spShadowMap = (*itor).m_spShadowMap;

            world = XMLoadFloat4x4( &(*itor).m_World );

            XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
            XMMATRIX viewProj = view*proj;
            XMMATRIX worldViewProj = world*view*proj;

            XMMATRIX texTransform = XMLoadFloat4x4( &(*itor).m_TexTransform );

            D3DX11_TECHNIQUE_DESC techDesc;
            pTech->GetDesc( &techDesc );

            for ( UINT p = 0; p < techDesc.Passes; ++p )
            {

                pElem->m_spShader->SetWorld( world );
                pElem->m_spShader->SetWorldViewProj( worldViewProj );
                pElem->m_spShader->SetViewProj( viewProj );
                pElem->m_spShader->SetWorldInvTranspose( worldInvTranspose );
                pElem->m_spShader->SetTexTransform( texTransform );
                pElem->m_spShader->SetShadowTransform( world*shadowTransform );
                pElem->m_spShader->SetMaterial( (*itor).m_mat );

                if ( spDiffuseMap )
                {
                    if ( !spDiffuseMap->IsArray() )
                        pElem->m_spShader->SetDiffuseMap( spDiffuseMap->GetSRV() );
                    else
                        pElem->m_spShader->SetDiffuseMapArray( spDiffuseMap->GetSRV() );
                }

                if ( spCubeMap )
                {
                    pElem->m_spShader->SetCubeMap( spCubeMap->GetSRV() );
                }

                if ( spNormalMap )
                {
                    pElem->m_spShader->SetNormalMap( spNormalMap->GetSRV() );
                }

                if ( spShadowMap )
                {
                    pElem->m_spShader->SetShadowMap( spShadowMap->GetSRV() );
                }

                pTech->GetPassByIndex( p )->Apply( 0, GetD3D11DeviceImmContext() );

                if ( pElem->m_bDrawIndex )
                {

                    GetD3D11DeviceImmContext()->DrawIndexed( (*itor).m_IndexCount, (*itor).m_StartIndexLocation, (*itor).m_BaseVertexLocation );
                }
                else
                {
                    GetD3D11DeviceImmContext()->Draw( (*itor).m_VertexCount, (*itor).m_StartVertexLocation );
                }
            }


            if ( pElem->m_bStencilReflect )
            {
                m_DirLights[0].Direction = oldLightDirections;
            }
        }
    }

    DrawScreenQuad();
}