Esempio n. 1
0
void Sky::Draw(ID3D11DeviceContext* dc, const Camera& camera)
{
	// center Sky about eye in world space
	XMFLOAT3 eyePos = camera.GetPosition();
	XMMATRIX T = XMMatrixTranslation(eyePos.x, eyePos.y, eyePos.z);


	XMMATRIX WVP = XMMatrixMultiply(T, camera.ViewProj());

	Effects::SkyFX->SetWorldViewProj(WVP);
	Effects::SkyFX->SetCubeMap(mCubeMapSRV);


	UINT stride = sizeof(XMFLOAT3);
    UINT offset = 0;
    dc->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
	dc->IASetIndexBuffer(mIB, DXGI_FORMAT_R16_UINT, 0);
	dc->IASetInputLayout(InputLayouts::Pos);
	dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	
	D3DX11_TECHNIQUE_DESC techDesc;
    Effects::SkyFX->SkyTech->GetDesc( &techDesc );

    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
        ID3DX11EffectPass* pass = Effects::SkyFX->SkyTech->GetPassByIndex(p);

		pass->Apply(0, dc);

		dc->DrawIndexed(mIndexCount, 0, 0);
	}
}
Esempio n. 2
0
	void D3D11EffectMaterial::ApplyPass(int index)
	{
		ID3DX11EffectPass* pPass = m_pTech->GetPassByIndex(index);

		pPass->Apply(0, m_pContext);

	}
Esempio n. 3
0
void GpuWaves::Disturb(ID3D11DeviceContext* dc, UINT i, UINT j, float magnitude)
{
	D3DX11_TECHNIQUE_DESC techDesc;

	// The grid element to displace.
	Effects::WaveSimFX->SetDisturbIndex(i, j);

	// The magnitude of the displacement.
	Effects::WaveSimFX->SetDisturbMag(magnitude);

	// Displace the current solution heights to generate a wave.
	Effects::WaveSimFX->SetCurrSolOutput(mWavesCurrSolUAV);

	Effects::WaveSimFX->DisturbWavesTech->GetDesc( &techDesc );
	for(UINT p = 0; p < techDesc.Passes; ++p)
	{
		ID3DX11EffectPass* pass = Effects::WaveSimFX->DisturbWavesTech->GetPassByIndex(p);
		pass->Apply(0, dc);

		// One thread group kicks off one thread, which displaces the height of one
		// vertex and its neighbors.
		dc->Dispatch(1, 1, 1);
	}

	// Unbind output from compute shader so we can use it as a shader input (a resource cannot be bound
	// as an output and input).
	ID3D11UnorderedAccessView* nullUAV[1] = { 0 };
	dc->CSSetUnorderedAccessViews( 0, 1, nullUAV, 0 );
}
Esempio n. 4
0
void GaussianMain::ShowImage(ID3D11DeviceContext* pd3dImmediateContext)
{
	// Output to default render target
	ID3D11RenderTargetView* rtv_array[1] = {g_pRTV_Default};
	pd3dImmediateContext->OMSetRenderTargets(1, rtv_array, g_pDSV_Default);
	pd3dImmediateContext->RSSetViewports(1, g_VP_Default);

	ID3DX11EffectTechnique* pTech = g_pFX_Render->GetTechniqueByName("Tech_ShowImage");
	ID3DX11EffectPass* pPass = g_bColorBlur ? pTech->GetPassByName("Pass_ShowColorImage") : pTech->GetPassByName("Pass_ShowMonoImage");

	if (g_bColorBlur)
		g_pFX_Render->GetVariableByName("g_texColorInput")->AsShaderResource()->SetResource(g_pSRV_Output);
	else
		g_pFX_Render->GetVariableByName("g_texMonoInput")->AsShaderResource()->SetResource(g_pSRV_Output);

	UINT strides = sizeof(SCREEN_VERTEX);
	UINT offsets = 0;
	ID3D11Buffer* pBuffers[1] = {g_pScreenQuadVB};

	pd3dImmediateContext->IASetInputLayout(g_pQuadLayout);
	pd3dImmediateContext->IASetVertexBuffers(0, 1, pBuffers, &strides, &offsets);
	pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

	pPass->Apply(0, pd3dImmediateContext);
	pd3dImmediateContext->Draw( 4, 0 );
}
void RenderTerrain(ID3D11DeviceContext* pContext, const D3DXMATRIX& mProj, const D3D11_VIEWPORT& vp, const char* passOverride=NULL)
{
	
	SetMatrices(GetApp()->ActiveCam_, mProj);

	g_HwTessellation = false;
	

	if (g_HwTessellation)
	{
		pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_4_CONTROL_POINT_PATCHLIST);
		pContext->IASetIndexBuffer(g_TileQuadListIB, DXGI_FORMAT_R32_UINT, 0);
	}
	else
	{
		pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
		pContext->IASetIndexBuffer(g_TileTriStripIB, DXGI_FORMAT_R32_UINT, 0);
	}

	const char* passName = "ShadedTriStrip";

	const bool wire = true;
	if (wire)
		passName = "Wireframe";
	if (g_HwTessellation)
	{
		if (wire)
			passName = "HwTessellatedWireframe";
		else
			passName = "HwTessellated";
	}
	if (passOverride != NULL)
		passName = passOverride;

	ID3DX11EffectPass* pPass = g_pTesselationTechnique->GetPassByName(passName);
	if (!pPass)
		return;		// Shouldn't happen unless the FX file is broken (like wrong pass name).

	SetViewport(pContext, vp);

	for (int i=0; i!=g_nRings ; ++i)
	{
		const TileRing* pRing = g_pTileRings[i];
		pRing->SetRenderingState(pContext);

		g_HeightMapVar->SetResource(g_pHeightMapSRV);
		g_GradientMapVar->SetResource(g_pGradientMapSRV);
		g_pTileSizeVar->SetFloat(pRing->tileSize());

		// Need to apply the pass after setting its vars.
		pPass->Apply(0, pContext);

		// Instancing is used: one tiles is one instance and the index buffer describes all the 
		// NxN patches within one tile.
		const int nIndices = (g_HwTessellation)? QUAD_LIST_INDEX_COUNT: TRI_STRIP_INDEX_COUNT;
		pContext->DrawIndexedInstanced(nIndices, pRing->nTiles(), 0, 0, 0);
	}
}
Esempio n. 6
0
void Terrain::Draw(ID3D11DeviceContext* dc, const Camera& cam, DirectionalLight lights[3])
{
	dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_4_CONTROL_POINT_PATCHLIST);
	dc->IASetInputLayout(InputLayouts::Terrain);

	UINT stride = sizeof(Vertex::Terrain);
    UINT offset = 0;
    dc->IASetVertexBuffers(0, 1, &mQuadPatchVB, &stride, &offset);
	dc->IASetIndexBuffer(mQuadPatchIB, DXGI_FORMAT_R16_UINT, 0);

	XMMATRIX viewProj = cam.ViewProj();
	XMMATRIX world  = XMLoadFloat4x4(&mWorld);
	XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
	XMMATRIX worldViewProj = world*viewProj;
	XMMATRIX ShadowTransform = world * XMLoadFloat4x4(&d3d->m_ShadowTransform);

	XMFLOAT4 worldPlanes[6];
	ExtractFrustumPlanes(worldPlanes, viewProj);

	// Set per frame constants.
	Effects::TerrainFX->SetViewProj(viewProj);
	Effects::TerrainFX->SetEyePosW(cam.GetPosition());
	Effects::TerrainFX->SetDirLights(lights);
	Effects::TerrainFX->SetFogColor(Colors::Silver);
	Effects::TerrainFX->SetFogStart(15.0f);
	Effects::TerrainFX->SetFogRange(175.0f);
	Effects::TerrainFX->SetMinDist(20.0f);
	Effects::TerrainFX->SetMaxDist(400.0f);
	Effects::TerrainFX->SetMinTess(0.0f);
	Effects::TerrainFX->SetMaxTess(6.0f);
	Effects::TerrainFX->SetTexelCellSpaceU(1.0f / mInfo.HeightmapWidth);
	Effects::TerrainFX->SetTexelCellSpaceV(1.0f / mInfo.HeightmapHeight);
	Effects::TerrainFX->SetWorldCellSpace(mInfo.CellSpacing);
	Effects::TerrainFX->SetWorldFrustumPlanes(worldPlanes);
	
	Effects::TerrainFX->SetLayerMapArray(mLayerMapArraySRV);
	Effects::TerrainFX->SetBlendMap(mBlendMapSRV);
	Effects::TerrainFX->SetHeightMap(mHeightMapSRV);
	Effects::TerrainFX->SetShadowMap(d3d->GetShadowMap());
	Effects::TerrainFX->SetShadowTransform(ShadowTransform);

	Effects::TerrainFX->SetMaterial(mMat);

	ID3DX11EffectTechnique* tech = Effects::TerrainFX->Light1Tech;
    D3DX11_TECHNIQUE_DESC techDesc;
    tech->GetDesc( &techDesc );

    for(UINT i = 0; i < techDesc.Passes; ++i)
    {
        ID3DX11EffectPass* pass = tech->GetPassByIndex(i);
		pass->Apply(0, dc);

		dc->DrawIndexed(mNumPatchQuadFaces*4, 0, 0);
	}	

	dc->HSSetShader(0, 0, 0);
	dc->DSSetShader(0, 0, 0);
}
Esempio n. 7
0
void EffectSystemD3D11::ApplyEffect(int effectID, const char *techName, const char *passName)
{
	ASSERT_EFFECT(effectID);
	ID3DX11EffectPass *pass = nullptr;
	pass = mEffects[effectID]->GetTechniqueByName(techName)->GetPassByName(passName);
	if (pass->IsValid())
	{
		pass->Apply(0, mContext);
	}
}
Esempio n. 8
0
HRESULT Shader::Apply( unsigned int pass )
{
	ID3DX11EffectPass* p = this->zTechnique->GetPassByIndex(pass);
	if (p)
	{
		p->Apply(0, this->zDeviceContext);

		if (this->zInputLayout)
		{
			this->zDeviceContext->IASetInputLayout(this->zInputLayout);
		}
		return S_OK;
	}
	return E_FAIL;
}
Esempio n. 9
0
HRESULT Shader::Apply(unsigned int pass)
{
	ID3DX11EffectPass* p = m_pTechnique->GetPassByIndex(pass);
	if(p)
	{
		p->Apply(0, mDevice3D->GetDeviceContext());

		if(m_pInputLayout)
		{
			mDevice3D->GetDeviceContext()->IASetInputLayout(m_pInputLayout);
		}
		return S_OK;
	}

	return E_FAIL;
}
void RawDataViewer::ShowRawVoxeldata(ID3D11ShaderResourceView*	pData, float fIndex)
{
	// Find a slice by an index, and then render it on a full-screen quad.
	mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	mTargetVar->SetResource(pData);
	mIndexVar->SetFloat(fIndex);

	mDeviceContext->IASetInputLayout(mVertexSlipLayout);
	UINT stride = sizeof(VertexSlip);
	UINT offset = 0;
	mDeviceContext->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
	ID3DX11EffectPass* pass = mRttTech->GetPassByIndex(0);

	pass->Apply(0, mDeviceContext);
	mDeviceContext->Draw(6, 0);

}
Esempio n. 11
0
void DX11SpriteBatch::EndBatch(ID3D11DeviceContext* dc)
{
	assert(mInitialized);

	u32 viewportCount = 1;
	D3D11_VIEWPORT vp;
	dc->RSGetViewports(&viewportCount, &vp);

	mScreenWidth  = vp.Width;
	mScreenHeight = vp.Height;

	u32 stride = sizeof(MeshData::SpriteVertex);
	u32 offset = 0;
	dc->IASetInputLayout(mInputLayout);
	dc->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
	dc->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);
	dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	DX11Effects::SpriteFX->SetSpriteMap(mBatchTexSRV);

	ID3DX11EffectPass* pass = DX11Effects::SpriteFX->SpriteTech->GetPassByIndex(0);
	pass->Apply(0, dc);

	u32 spritesToDraw = (u32)mSpriteList.size();
	u32 startIndex = 0;

	while( spritesToDraw > 0 )
	{
		if( spritesToDraw <= BatchSize )
		{
			DrawBatch(dc, startIndex, spritesToDraw);
			spritesToDraw = 0;
		}
		else
		{
			DrawBatch(dc, startIndex, BatchSize);
			startIndex += BatchSize;
			spritesToDraw -= BatchSize;
		}
	}

	RJE_SAFE_RELEASE(mBatchTexSRV);
}
Esempio n. 12
0
void h2GBufferDebugDraw::RenderGBuffer() {
	m_Renderer->m_D3d11ImmediateContext->ClearDepthStencilView( m_Renderer->m_DepthBufferDSV, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 );
	m_Renderer->m_D3d11ImmediateContext->IASetInputLayout( m_PosTexLayout );
	m_Renderer->m_D3d11ImmediateContext->OMSetRenderTargets( 1, &m_Renderer->m_BackbufferRTV, m_Renderer->m_DepthBufferDSV );

	ID3DX11EffectShaderResourceVariable * fxTexSamplerVar;
	fxTexSamplerVar = m_Fx->GetVariableByName( "texSampler" )->AsShaderResource();
	fxTexSamplerVar->SetResource( m_Renderer->m_GBuffer->GetBufferByType( m_CurrentBuffer )->m_SRV );

	unsigned int stride = sizeof( h2RenderingEngine::vertexPosTex_t );
	unsigned int offset = 0;
	m_Renderer->m_D3d11ImmediateContext->IASetVertexBuffers( 0, 1, &m_Renderer->m_FullScrQuadVB, &stride, &offset );

	const h2SceneObject * cameraObj = h2Engine::GetScene()->GetMainCamera();
	const h2Camera * camera = static_cast<h2Camera *>( cameraObj->GetComponent( "h2Camera" ) );
	h2Matrix44 projMatrix = h2Matrix44::OrthographicD3D( m_Renderer->m_Width, m_Renderer->m_Height, camera->zNear, camera->zFar );

	ID3DX11EffectMatrixVariable * fxPVar = m_Fx->GetVariableByName( "orthoProjection" )->AsMatrix();
	fxPVar->SetMatrix( projMatrix.GetDataPtr() );

	ID3DX11EffectTechnique * tech = nullptr;
	switch ( m_CurrentBuffer ) {
	case DEPTH_BUFFER:
		tech = m_Fx->GetTechniqueByName( "DepthBufferDebugDrawTech" );
		break;
	case NORMAL_BUFFER:
		tech = m_Fx->GetTechniqueByName( "NormalBufferDebugDrawTech" );
		break;
	}

	ID3DX11EffectPass * pass = tech->GetPassByIndex( 0 );
	pass->Apply( 0, m_Renderer->m_D3d11ImmediateContext );

	m_Renderer->m_D3d11ImmediateContext->Draw( 6, 0 );

	ID3D11ShaderResourceView * emptySRV = nullptr;
	m_Renderer->m_D3d11ImmediateContext->PSSetShaderResources(0, 1, &emptySRV);
}
Esempio n. 13
0
void Entity::DrawShadow(ID3DX11EffectTechnique* activeTech, ID3D11DeviceContext* context, const Camera& camera, XMFLOAT4X4 lightView, XMFLOAT4X4 lightProj)
{
	XMMATRIX view		= XMLoadFloat4x4(&lightView);
	XMMATRIX proj		= XMLoadFloat4x4(&lightProj);
	XMMATRIX viewProj	= XMMatrixMultiply(view, proj);

	ID3DX11EffectTechnique* smapTech = Effects::BuildShadowMapFX->BuildShadowMapAlphaClipTech;
	D3DX11_TECHNIQUE_DESC techDesc;
	smapTech->GetDesc(&techDesc);
	for (UINT p = 0; p < techDesc.Passes; ++p)
	{
		ID3DX11EffectPass* pass = activeTech->GetPassByIndex(p);
		XMMATRIX world = XMLoadFloat4x4(&mWorld);
		XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world*view*proj;
		Effects::BuildShadowMapFX->SetWorld(world);
		Effects::BuildShadowMapFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::BuildShadowMapFX->SetWorldViewProj(worldViewProj);
		Effects::BuildShadowMapFX->SetDiffuseMap(mTexSRV);

		if (!useTexTrans){ Effects::BuildShadowMapFX->SetTexTransform(XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z)); }
		if (mBasicTexTrans)
		{
			Effects::BuildShadowMapFX->SetTexTransform(XMMatrixTranslation(texTrans.x, texTrans.y, texTrans.z)*XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z));
		}
		if (mUseAnimation)
		{
			XMMATRIX Scale;
			mAnim->Flipped() ? Scale = XMMatrixScaling(-origTexScale.x, origTexScale.y, origTexScale.z) : Scale = XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z);
			Effects::BuildShadowMapFX->SetTexTransform(XMMatrixTranslation(mAnim->GetX(), mAnim->GetY(), texTrans.z)*Scale);
		}

		pass->Apply(0, context);
		context->DrawIndexed(mIndexCount, mIndexOffset, mVertexOffset);
	}
}
Esempio n. 14
0
void GpuWaves::Update(ID3D11DeviceContext* dc, float dt)
{
	static float t = 0;

	// Accumulate time.
	t += dt;

	// Only update the simulation at the specified time step.
	if( t >= mTimeStep )
	{
		D3DX11_TECHNIQUE_DESC techDesc;
		Effects::WaveSimFX->SetWaveConstants(mK);

		Effects::WaveSimFX->SetPrevSolInput(mWavesPrevSolSRV);
		Effects::WaveSimFX->SetCurrSolInput(mWavesCurrSolSRV);
		Effects::WaveSimFX->SetNextSolOutput(mWavesNextSolUAV);

		Effects::WaveSimFX->UpdateWavesTech->GetDesc( &techDesc );
		for(UINT p = 0; p < techDesc.Passes; ++p)
		{
			ID3DX11EffectPass* pass = Effects::WaveSimFX->UpdateWavesTech->GetPassByIndex(p);
			pass->Apply(0, dc);

			// How many groups do we need to dispatch to cover the wave grid.  
			// Note that mNumRows and mNumCols should be divisible by 16
			// so there is no remainder.
			UINT numGroupsX = mNumCols / 16;
			UINT numGroupsY = mNumRows / 16;
			dc->Dispatch(numGroupsX, numGroupsY, 1);
		}

		// Unbind the input textures from the CS for good housekeeping.
		ID3D11ShaderResourceView* nullSRV[1] = { 0 };
		dc->CSSetShaderResources( 0, 1, nullSRV );

		// Unbind output from compute shader (we are going to use this output as an input in the next pass, 
		// and a resource cannot be both an output and input at the same time.
		ID3D11UnorderedAccessView* nullUAV[1] = { 0 };
		dc->CSSetUnorderedAccessViews( 0, 1, nullUAV, 0 );

		// Disable compute shader.
		dc->CSSetShader(0, 0, 0);

		//
		// Ping-pong buffers in preparation for the next update.
		// The previous solution is no longer needed and becomes the target of the next solution in the next update.
		// The current solution becomes the previous solution.
		// The next solution becomes the current solution.
		//

		ID3D11ShaderResourceView* srvTemp = mWavesPrevSolSRV;
		mWavesPrevSolSRV = mWavesCurrSolSRV;
		mWavesCurrSolSRV = mWavesNextSolSRV;
		mWavesNextSolSRV = srvTemp;

		ID3D11UnorderedAccessView* uavTemp = mWavesPrevSolUAV;
		mWavesPrevSolUAV = mWavesCurrSolUAV;
		mWavesCurrSolUAV = mWavesNextSolUAV;
		mWavesNextSolUAV = uavTemp;

		t = 0.0f; // reset time
	}
}
Esempio n. 15
0
void EndOfDirectX11::Render()
{
	if( d3dContext_ == 0 )
	{
		return;
	}

	float clearColor[] = { 0.0f, 0.0f, 0.25f, 1.0f };
	d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );
	d3dContext_->ClearDepthStencilView( depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0 );     

	unsigned int stride = sizeof( VertexPos );
	unsigned int offset = 0;

	d3dContext_->IASetInputLayout( inputLayout_ );
	d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset );
	d3dContext_->IASetIndexBuffer( indexBuffer_, DXGI_FORMAT_R16_UINT, 0 );
	d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

	ID3DX11EffectShaderResourceVariable * colorMap = 0;
	colorMap = effect_->GetVariableByName( "colorMap" )->AsShaderResource();
	colorMap->SetResource( colorMap_ );

	ID3DX11EffectShaderResourceVariable* colorMap2;
    colorMap2 = effect_->GetVariableByName( "secondMap" )->AsShaderResource( );
    colorMap2->SetResource( secondMap_ );

	ID3DX11EffectSamplerVariable * samplerState = 0;
	samplerState = effect_->GetVariableByName( "colorSampler" )->AsSampler();
	samplerState->SetSampler( 0, samplerState_ );

	ID3DX11EffectMatrixVariable * worldMatrix = 0;
	worldMatrix = effect_->GetVariableByName( "worldMatrix" )->AsMatrix();
	worldMatrix->SetMatrix( ( float * )&worldMat_ );

	ID3DX11EffectMatrixVariable * viewMatrix = 0;
	viewMatrix = effect_->GetVariableByName( "viewMatrix" )->AsMatrix();
	viewMatrix->SetMatrix( ( float * )&viewMatrix_ );

	ID3DX11EffectMatrixVariable * projMatrix = 0;
	projMatrix = effect_->GetVariableByName( "projMatrix" )->AsMatrix();
	projMatrix->SetMatrix( ( float * )&projMatrix_ );

	ID3DX11EffectTechnique * colorInvTechnique = 0;
	colorInvTechnique = effect_->GetTechniqueByName( "MultiTexture" );

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

	for( unsigned int p = 0; p < techDesc.Passes; ++p )
	{
		ID3DX11EffectPass * pass = colorInvTechnique->GetPassByIndex( p );

		if( pass != 0 )
		{
			pass->Apply( 0, d3dContext_ );
			d3dContext_->DrawIndexed( 36, 0, 0 );
		}
	}

	swapChain_->Present( 0, 0 );
}
Esempio n. 16
0
void App::drawScene( void )
{
    mD3DImmediateContext->ClearRenderTargetView( mRenderTargetView,
                                                 reinterpret_cast<const float*>( &Colors::Black ) );
    mD3DImmediateContext->ClearDepthStencilView( mDepthStencilView,
                                                 D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,
                                                 1.f,
                                                 0 );

    mD3DImmediateContext->IASetInputLayout( InputLayouts::Basic32 );
    mD3DImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

    float blendFactor [] = { 0.f, 0.f, 0.f, 0.f };
    const UINT stride = sizeof( Vertex::Basic32 );
    const UINT offset = 0;

    XMMATRIX view = XMLoadFloat4x4( &mView );
    XMMATRIX proj = XMLoadFloat4x4( &mProj );
    const XMMATRIX viewProj = view * proj;

    // Set per-frame constants.
    Effects::BasicFX->SetDirLights( mDirLights );
    Effects::BasicFX->SetEyePosW( mEyePosW );
    Effects::BasicFX->SetFogColor( Colors::Black );
    Effects::BasicFX->SetFogStart( 2.f );
    Effects::BasicFX->SetFogRange( 40.f );

    ID3DX11EffectTechnique* tech = Effects::BasicFX->Light0TexTech;
    ID3DX11EffectTechnique* activeSkullTech = Effects::BasicFX->Light0TexTech;

    switch ( mRenderOptions )
    {
    case RenderOptions::Lighting:
        tech = Effects::BasicFX->Light3Tech;
        activeSkullTech = Effects::BasicFX->Light3Tech;
        break;
    case RenderOptions::Textures:
        tech = Effects::BasicFX->Light3TexTech;
        activeSkullTech = Effects::BasicFX->Light3Tech;
        break;
    case RenderOptions::TexturesAndFog:
        tech = Effects::BasicFX->Light3TexFogTech;
        activeSkullTech = Effects::BasicFX->Light3FogTech;
        break;
    }

    D3DX11_TECHNIQUE_DESC techDesc;

    // 
    // Draw floor and walls to back buffer as normal.

    tech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p ) {
        ID3DX11EffectPass* pass = tech->GetPassByIndex( p );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset );
        // ???

        // Set per-object constants.
        XMMATRIX world = XMLoadFloat4x4( &mRoomWorld );
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world * viewProj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetTexTransform( XMMatrixIdentity() );
        Effects::BasicFX->SetMaterial( mRoomMat );

        // Floor.
        Effects::BasicFX->SetDiffuseMap( mFloorDiffuseMapSRV );
        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->Draw( 6, 0 );

        // Wall.
        Effects::BasicFX->SetDiffuseMap( mWallDiffuseMapSRV );
        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->Draw( 18, 6 );
    }

    //
    // Draw the skull to the back buffer as normal.

    activeSkullTech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p ) {
        ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset );
        mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 );

        XMMATRIX world = XMLoadFloat4x4( &mSkullWorld );
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world*view*proj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetMaterial( mSkullMat );

        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 );
    }

    //
    // Draw the mirror into the stencil buffer only.

    tech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p ) {
        ID3DX11EffectPass* pass = tech->GetPassByIndex( 0 );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset );

        // Set per object constants.
        XMMATRIX world = XMLoadFloat4x4( &mRoomWorld );
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world*view*proj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetTexTransform( XMMatrixIdentity() );

        // Do not write to render target.
        mD3DImmediateContext->OMSetBlendState( RenderStates::NoRenderTargetWritesBS,
                                               blendFactor,
                                               0xffffffff );

        // Render visible mirror pixels to stencil buffer.
        // Do not write mirror depth to depth buffer to avoid occluding the reflection.
        mD3DImmediateContext->OMSetDepthStencilState( RenderStates::MarkMirrorDSS,
                                                      1 );

        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->Draw( 6, 24 );

        // Restore states.
        mD3DImmediateContext->OMSetBlendState( nullptr, blendFactor, 0xffffffff );
        mD3DImmediateContext->OMSetDepthStencilState( nullptr, 0 );
    }

    //
    // Draw skull reflection to back buffer.

    activeSkullTech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p ) {
        ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset );
        mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 );

        XMVECTOR mirrorPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); // xy plane
        XMMATRIX R = XMMatrixReflect( mirrorPlane );
        //XMMATRIX world = XMMatrixRotationY( MathHelper::Pi ) * XMLoadFloat4x4( &mSkullWorld )  * R;
        XMMATRIX world = XMLoadFloat4x4( &mSkullWorld ) * R;
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world*view*proj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetMaterial( mSkullMat );

        // Cache old light directions, then reflect them.
        XMFLOAT3 oldLightDirs[3];
        for ( int i = 0; i < 3; ++i ) {
            oldLightDirs[i] = mDirLights[i].direction;

            XMVECTOR dir = XMLoadFloat3( &mDirLights[i].direction );
            XMVECTOR reflection = XMVector3TransformNormal( dir, R );
            XMStoreFloat3( &mDirLights[i].direction, reflection );
        }

        Effects::BasicFX->SetDirLights( mDirLights );

        // Cull clockwise triangles for reflection.
        mD3DImmediateContext->RSSetState( RenderStates::CullClockwiseRS );

        // Only draw reflection into visible mirror pixels as marked by stencil buffer.
        mD3DImmediateContext->OMSetDepthStencilState( RenderStates::DrawReflectionDSS,
                                                      1 );
        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 );

        // Restore default states.
        mD3DImmediateContext->RSSetState( nullptr );
        mD3DImmediateContext->OMSetDepthStencilState( nullptr, 0 );

        // Restore light directions.
        for ( int i = 0; i < 3; ++i )
        {
            mDirLights[i].direction = oldLightDirs[i];
        }

        Effects::BasicFX->SetDirLights( mDirLights );
    }

    // Draw mirror to back buffer.
    tech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p )
    {
        ID3DX11EffectPass* pass = tech->GetPassByIndex( p );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset );

        // Set per object constants.
        XMMATRIX world = XMLoadFloat4x4( &mRoomWorld );
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world*view*proj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetTexTransform( XMMatrixIdentity() );
        Effects::BasicFX->SetMaterial( mMirrorMat );
        Effects::BasicFX->SetDiffuseMap( mMirrorDiffuseMapSRV );

        // Mirror
        mD3DImmediateContext->OMSetBlendState( RenderStates::TransparentBS, blendFactor, 0xffffffff );
        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->Draw( 6, 24 );
    }

    // Draw skull shadow.
    activeSkullTech->GetDesc( &techDesc );
    for ( UINT p = 0; p < techDesc.Passes; ++p ) {
        ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p );

        mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset );
        mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 );

        XMVECTOR shadowPlane = XMVectorSet( 0.f, 1.f, 0.f, 0.f );
        XMVECTOR toMainLight = -XMLoadFloat3( &mDirLights[0].direction );
        XMMATRIX S = XMMatrixShadow( shadowPlane, toMainLight );
        XMMATRIX shadowOffsetY = XMMatrixTranslation( 0.f, 0.001f, 0.f );

        // Set per-object constants.
        XMMATRIX world = XMLoadFloat4x4( &mSkullWorld ) * S * shadowOffsetY;
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world );
        XMMATRIX worldViewProj = world*view*proj;

        Effects::BasicFX->SetWorld( world );
        Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose );
        Effects::BasicFX->SetWorldViewProj( worldViewProj );
        Effects::BasicFX->SetMaterial( mShadowMat );

        mD3DImmediateContext->OMSetDepthStencilState( RenderStates::NoDoubleBlendDSS,
                                                      0 );
        pass->Apply( 0, mD3DImmediateContext );
        mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 );

        // Restore default states.
        mD3DImmediateContext->OMSetBlendState( 0, blendFactor, 0xffffffff );
        mD3DImmediateContext->OMSetDepthStencilState( 0, 0 );
    }

    HR( mSwapChain->Present( 0, 0 ) );
}
Esempio n. 17
0
void GaussianMain::ApplyGaussianFilter(ID3D11DeviceContext* pd3dImmediateContext)
{
	D3D11_TEXTURE2D_DESC tex_desc;
	g_pTex_Scene->GetDesc(&tex_desc);

	float box_width = CalculateBoxFilterWidth(g_FilterRadius, g_NumApproxPasses);
	float half_box_width = box_width * 0.5f;
	float frac_half_box_width = (half_box_width + 0.5f) - (int)(half_box_width + 0.5f);
	float inv_frac_half_box_width = 1.0f - frac_half_box_width;
	float rcp_box_width = 1.0f / box_width;


	// Step 1. Vertical passes: Each thread group handles a colomn in the image

	ID3DX11EffectTechnique* pTech = g_pFX_GaussianCol->GetTechniqueByName("Tech_GaussianFilter");

	// Input texture
	g_pFX_GaussianCol->GetVariableByName("g_texInput")->AsShaderResource()->SetResource(g_pSRV_Scene);
	// Output texture
	g_pFX_GaussianCol->GetVariableByName("g_rwtOutput")->AsUnorderedAccessView()->SetUnorderedAccessView(g_pUAV_Output);

	g_pFX_GaussianCol->GetVariableByName("g_NumApproxPasses")->AsScalar()->SetInt(g_NumApproxPasses - 1);
	g_pFX_GaussianCol->GetVariableByName("g_HalfBoxFilterWidth")->AsScalar()->SetFloat(half_box_width);
	g_pFX_GaussianCol->GetVariableByName("g_FracHalfBoxFilterWidth")->AsScalar()->SetFloat(frac_half_box_width);
	g_pFX_GaussianCol->GetVariableByName("g_InvFracHalfBoxFilterWidth")->AsScalar()->SetFloat(inv_frac_half_box_width);
	g_pFX_GaussianCol->GetVariableByName("g_RcpBoxFilterWidth")->AsScalar()->SetFloat(rcp_box_width);

	// Select pass
	ID3DX11EffectPass* pPass = g_bColorBlur ? pTech->GetPassByName("Pass_GaussianColor") : pTech->GetPassByName("Pass_GaussianMono");

	pPass->Apply(0, pd3dImmediateContext);
	pd3dImmediateContext->Dispatch(tex_desc.Width, 1, 1);

	// Unbound CS resource and output
	ID3D11ShaderResourceView* srv_array[] = {NULL, NULL, NULL, NULL};
	pd3dImmediateContext->CSSetShaderResources(0, 4, srv_array);
	ID3D11UnorderedAccessView* uav_array[] = {NULL, NULL, NULL, NULL};
	pd3dImmediateContext->CSSetUnorderedAccessViews(0, 4, uav_array, NULL);


	// Step 2. Horizontal passes: Each thread group handles a row in the image

	pTech = g_pFX_GaussianRow->GetTechniqueByName("Tech_GaussianFilter");

	// Input texture
	g_pFX_GaussianRow->GetVariableByName("g_texInput")->AsShaderResource()->SetResource(g_pSRV_Scene);
	// Output texture
	g_pFX_GaussianRow->GetVariableByName("g_rwtOutput")->AsUnorderedAccessView()->SetUnorderedAccessView(g_pUAV_Output);

	g_pFX_GaussianRow->GetVariableByName("g_NumApproxPasses")->AsScalar()->SetInt(g_NumApproxPasses - 1);
	g_pFX_GaussianRow->GetVariableByName("g_HalfBoxFilterWidth")->AsScalar()->SetFloat(half_box_width);
	g_pFX_GaussianRow->GetVariableByName("g_FracHalfBoxFilterWidth")->AsScalar()->SetFloat(frac_half_box_width);
	g_pFX_GaussianRow->GetVariableByName("g_InvFracHalfBoxFilterWidth")->AsScalar()->SetFloat(inv_frac_half_box_width);
	g_pFX_GaussianRow->GetVariableByName("g_RcpBoxFilterWidth")->AsScalar()->SetFloat(rcp_box_width);

	// Select pass
	pPass = g_bColorBlur ? pTech->GetPassByName("Pass_GaussianColor") : pTech->GetPassByName("Pass_GaussianMono");

	pPass->Apply(0, pd3dImmediateContext);
	pd3dImmediateContext->Dispatch(tex_desc.Height, 1, 1);

	// Unbound CS resource and output
	pd3dImmediateContext->CSSetShaderResources(0, 4, srv_array);
	pd3dImmediateContext->CSSetUnorderedAccessViews(0, 4, uav_array, NULL);
}