Sign in to follow this  
mind in a box

[C++/Direct3D 10]Accessing the backbuffer in HLSL

Recommended Posts

Hi there! I was just wondering, how to access the backbuffer in HLSL. I actually wrote a function that should set it to the shader, but it isn't working:
void Shader::ResolutionChanged()
{
	if(BackBufferVariable)
	{		
		ID3D10RenderTargetView* RTV;
		DXUTGetD3D10Device()->OMGetRenderTargets(1,&RTV,NULL);

		BackBufferVariable->SetRenderTarget(RTV);
		RTV->Release();
	}
}

While BackBufferVariable is a
ID3D10EffectRenderTargetViewVariable *
further, I don't know how to declare the texture in HLSL. Do I just have to use something like this?:
Texture2D Scene;
And do I need to use a special sampler? Please help me fixing that. I'm making a post-processing interface at the moment, and I think i *could* need this! :D

Share this post


Link to post
Share on other sites
Shaders read from shader resource views and write to render target views. It doesn't work another way. Also, you can't set the same texture as both a resource view and render target at the same time.

The normal way to go about this would be to create a texture with both render target and resource views, render to it (using the render target view), then read from it (using the resource view) and write to the back buffer.

Share this post


Link to post
Share on other sites
Now I've got 2 more questions:

1. Is there really no way to access the current backbuffer in a shader? I've seen that in the UnrealEngine3, and it would be just awesome! :)

2. Can I set the ResourceView while it is the backbuffer? But without using it! Just set... Because I get only a black screen and DX says that:

D3D10: INFO: ID3D10Device::DrawIndexed: The Pixel Shader unit expects a Shader Resource View at Slot 0, but none is bound. This is OK, as reads of an unbound Shader Resource View are defined to return 0. It is also possible the developer knows the data will not be used anyway. This is only a problem if the developer actually intended to bind a Shader Resource View here.  [ EXECUTION INFO #353: DEVICE_DRAW_SHADERRESOURCEVIEW_NOT_SET ]


Share this post


Link to post
Share on other sites
No, you can't do read and write to the same resource in this manner. The only type of resource this capability is a UAV, which would require D3D11 support, or compute shader on DX10.1.

There's no way to do this and get consistent results because there would be no guarantees about reads/writes orders when a resource is bound as a RTV and a SRV and therefore the results would be undefined. For this reason the pipeline ensures that a resource is not bound for both at the same time.

Using a UAV could open up an opportunity to do things yourself, however you'd have to emulate any output merger functionality that you need. You would also have to manage the problem with read/write ordering yourself, which isn't going to be easy given that you don't have access to the active depth buffer either, and many of the atomics are not available in pixel shaders for compute on 10.

I don't know what the unreal engine is doing. They might have proprietary IHV hooks to make this happen, or perhaps their approach works differently than you expect. Most of this stuff is normally done with multiple render passes as a work around for this very restriction.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this