Blur computer shader, can't figured out warning

Started by
3 comments, last by cozzie 7 years, 9 months ago

Hi all,

I've been playing around with my 1st compute shader; a Blur demo (based on the D3D11 book of Frank Luna).

It's all working as planned when I draw the scene using textures, when I draw the scene using just lighting (no textures), the blur still works but I get this warning for each frame:


D3D11 WARNING: ID3D11DeviceContext::CSSetUnorderedAccessViews: Resource being set to CS UnorderedAccessView slot 0 is still bound on input! [ STATE_SETTING WARNING #2097354: DEVICE_CSSETUNORDEREDACCESSVIEWS_HAZARD]
D3D11 WARNING: ID3D11DeviceContext::CSSetUnorderedAccessViews: Forcing PS shader resource slot 0 to NULL. [ STATE_SETTING WARNING #7: DEVICE_PSSETSHADERRESOURCES_HAZARD]

I looked up what this could be, it could have something to do with not unbinding the SRV or UAV, before binding it again to the input or output of the computer shader. But I do that correctly (I think):


		// HORIZONTAL BLUR PASS
		for(UINT p=0;p<techDesc.Passes;++p)
		{
			Effects::mBlurFX->SetInputMap(pInputSRV);
			Effects::mBlurFX->SetOutputMap(mBlurredOutputTexUAV);
			Effects::mBlurFX->mHorzBlurTech->GetPassByIndex(p)->Apply(0, dc);

			// how many groups do we need to dispatch?
			// cover all rows of pixels, group size = 256 (defined in compute shader)
			UINT numGroupsX = (UINT)ceilf(mWidth / 256.0f);
			dc->Dispatch(numGroupsX, mHeight, 1);
		}

		// unbind input texture from the CS (housekeeping)
		ID3D11ShaderResourceView *nullSRV[1] = { 0 };
		dc->CSSetShaderResources(0, 1, nullSRV);

		// unbind output from compute shader (we will use this output as input in next step)
		ID3D11UnorderedAccessView *nullUAV[1] = { 0 };
		dc->CSSetUnorderedAccessViews(0, 1, nullUAV, 0);

		// VERTICAL BLUR PASS
		Effects::mBlurFX->mVertBlurTech->GetDesc(&techDesc);
		
		for(UINT p=0;p<techDesc.Passes;++p)
		{
			Effects::mBlurFX->SetInputMap(mBlurredOutputTexSRV);
			Effects::mBlurFX->SetOutputMap(pInputUAV);
			Effects::mBlurFX->mVertBlurTech->GetPassByIndex(p)->Apply(0, dc);

			// how many groups do we need to dispatch?
			// cover all columns of pixels, group size = 256 (defined in compute shader)
			UINT numGroupsY = (UINT)ceilf(mHeight / 256.0f);
			dc->Dispatch(mWidth, numGroupsY, 1);
		}

		dc->CSSetShaderResources(0, 1, nullSRV);
		dc->CSSetUnorderedAccessViews(0, 1, nullUAV, 0);

At first I though it might have to with the fact that I'm rendering the base scene not using textures and afterwards render the fullscreen quad (with blurred scene/ output texture) using a texture. But that doesn't make sense when I look at my shader, because rendering the scene itself without textures has a different technique as rendering the fullscreen quad with the texture.

Do you have any idea/ direction where I should look to solve this warning?

I can paste more parts of the code if neccessary.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

That means that you have a resource that's bound as an SRV (input) for the pixel shader stage, and you're trying to bind it as a UAV (output) for the compute shader stage. The debug layer always complains about these situations because you're not allowed to have the same resource simultaneously bound as an input and an output. You'll just need to clear out the PS SRV's before you bind your CS UAV's, which you do by binding NULL pointers to the slots.

Thanks. I think I'm already doing that within my blur function (see the code paste above).

Or should I also do it within rendering the base scene initially, to a texture?

Strangely the warning only appears when I render the initial scene without textures.

This is the main draw code:


void BlurApp::DrawScene()
{
	ID3D11RenderTargetView *renderTargets[1] = { mOffscreenRTV };
	md3dImmediateContext->OMSetRenderTargets(1, renderTargets, mDepthStencilView);

	md3dImmediateContext->ClearRenderTargetView(mOffscreenRTV, reinterpret_cast<const float*>(&Colors::Silver));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	// draw scene to offscreen texture
	DrawWrapper();

	// restore backbuffer. offscreen RT will serve as input for compute shader, unbind it first
	renderTargets[0] = mRenderTargetView;
	md3dImmediateContext->OMSetRenderTargets(1, renderTargets, mDepthStencilView);

	// mBlur.SetGaussianWeights(4.0f);
	mBlur.BlurInPlace(md3dImmediateContext, mOffscreenSRV, mOffscreenUAV, 2);

	// draw fullscreen quad with blurred texture on it
	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Silver));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	DrawScreenQuad();

	HR(mSwapChain->Present(0, 0));
}

Here's the code of rendering the initial scene:


void BlurApp::DrawWrapper()
{
	md3dImmediateContext->IASetInputLayout(InputLayouts::mBasic32);
    md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	UINT stride = sizeof(Vertex::Basic32);
    UINT offset = 0;

	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
 
	XMMATRIX view  = XMLoadFloat4x4(&mView);
	XMMATRIX proj  = XMLoadFloat4x4(&mProj);
	XMMATRIX viewProj = view*proj;
	
	// per frame constants
	Effects::mBasicFX->SetDirLights(mDirLights);
	Effects::mBasicFX->SetEyePosW(mEyePosW);
	Effects::mBasicFX->SetFogColor(Colors::Silver);
	Effects::mBasicFX->SetFogStart(15.0f);
	Effects::mBasicFX->SetFogRange(175.0f);

	// figure out correct technique
	ID3DX11EffectTechnique* boxTech = NULL;					//Effects::mBasicFX->mLight3Tech; //mLight1TexTech;;
	ID3DX11EffectTechnique* landAndWavesTech = NULL;		//Effects::mBasicFX->mLight3Tech; //mLight1TexTech;
 
	switch(mRenderOptions)
	{
		case RenderOptions::Lighting:
			boxTech = Effects::mBasicFX->mLight3Tech;
			landAndWavesTech = Effects::mBasicFX->mLight3Tech;
			break;

		case RenderOptions::Textures:
			boxTech = Effects::mBasicFX->mLight3TexAlphaClipTech;
			landAndWavesTech = Effects::mBasicFX->mLight3TexTech;
			break;

		case RenderOptions::TexturesAndFog:
			boxTech = Effects::mBasicFX->mLight3TexAlphaClipFogTech;
			landAndWavesTech = Effects::mBasicFX->mLight3TexFogTech;
			break;
	}

	D3DX11_TECHNIQUE_DESC techDesc;

	// draw box
	boxTech->GetDesc(&techDesc);
	for(UINT p=0;p<techDesc.Passes;++p)
    {
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mBoxIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// Set per object constants.
		XMMATRIX world = XMLoadFloat4x4(&mBoxWorld);
		XMMATRIX worldInvTranspose = CMathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world * view * proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMMatrixIdentity());
		Effects::mBasicFX->SetMaterial(mBoxMat);
		Effects::mBasicFX->SetDiffuseMap(mBoxMapSRV);

		md3dImmediateContext->RSSetState(RenderStates::mNoCullRS);
		boxTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(36, 0, 0);

		// restore render state(s)
		md3dImmediateContext->RSSetState(0);
	}

	// draw hills and water, with texture + fog (no alpha clipping needed)
	landAndWavesTech->GetDesc(&techDesc);
    for(UINT p=0;p<techDesc.Passes;++p)
    {
		// hills first
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mLandVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mLandIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// per object shader constants
		XMMATRIX world = XMLoadFloat4x4(&mLandWorld);
		XMMATRIX worldInvTranspose = CMathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world*view*proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMLoadFloat4x4(&mGrassTexTransform));
		Effects::mBasicFX->SetMaterial(mLandMat);
		Effects::mBasicFX->SetDiffuseMap(mGrassMapSRV);

		landAndWavesTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(mLandIndexCount, 0, 0);

		// waves
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mWavesVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mWavesIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// Set per object constants.
		world = XMLoadFloat4x4(&mWavesWorld);
		worldInvTranspose = CMathHelper::InverseTranspose(world);
		worldViewProj = world * view * proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMLoadFloat4x4(&mWaterTexTransform));
		Effects::mBasicFX->SetMaterial(mWavesMat);
		Effects::mBasicFX->SetDiffuseMap(mWavesMapSRV);

		md3dImmediateContext->OMSetBlendState(RenderStates::mTransparentBS, blendFactor, 0xffffffff);
		landAndWavesTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(3 * mWaves.TriangleCount(), 0, 0);

		// restore (blend) states
		md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);
    }
	HR(mSwapChain->Present(0, 0));
}

Maybe I can set the rendertarget in the beginning of the draw scene to NULL first.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I don't see you setting the PS shader resources anywhere. You basically need to do this:

ID3D11ShaderResourceView* nullSRVs[1] = { nullptr };
context->PSSetShaderResourceViews(0, 1, nullSRVs);

Thanks, it works.

I've added this at the beginning of the DrawScene function.

I guess I'm not setting PS shader resources, because I'm using the Effects11 library, which probably does it for me.


void BlurApp::DrawScene()
{
	ID3D11ShaderResourceView* nullSRVs[1] = { nullptr };
	md3dImmediateContext->PSSetShaderResources(0, 1, nullSRVs);

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement