Sign in to follow this  
jdub

Compute Shader won't Fill Texture

Recommended Posts

jdub    459

I'm trying to set up a simple compute shader program which simply fills a given texture with pixel data of a certain color.  Here is my CPU side code:

bool Init(void)
{
	HRESULT res;
	D3D11_TEXTURE2D_DESC texture_desc;
	D3D11_SHADER_RESOURCE_VIEW_DESC texture_SRV_desc;
	D3D11_UNORDERED_ACCESS_VIEW_DESC texture_UAV_desc;


	texture_desc.ArraySize = 1;
	texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
	texture_desc.CPUAccessFlags = 0;
	texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	texture_desc.Width = this->image_width;
	texture_desc.Height = this->image_height;
	texture_desc.MipLevels = 1;
	texture_desc.MiscFlags = 0;
	texture_desc.SampleDesc.Count = 1; 
	texture_desc.SampleDesc.Quality = 0;
	texture_desc.Usage = D3D11_USAGE_DEFAULT;

	texture_SRV_desc.Texture2D.MipLevels = 1;
	texture_SRV_desc.Texture2D.MostDetailedMip = 0;
	texture_SRV_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	texture_SRV_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

	texture_UAV_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	texture_UAV_desc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
	texture_UAV_desc.Texture2D.MipSlice = 0;
	
	if (FAILED(res = this->renderer->GetDevice()->CreateTexture2D(&texture_desc, NULL, &this->texture)))
	{
		return false;
	}
	
	if (FAILED(res = this->renderer->GetDevice()->CreateUnorderedAccessView(this->texture, &texture_UAV_desc, &this->texture_uav)))
	{
		return false;
	}

	if (FAILED(res = this->renderer->GetDevice()->CreateShaderResourceView(this->texture, &texture_SRV_desc, &this->texture_SRV)))
	{
		return false;
	}

	if (!this->create_compute_shader())
		return false;

	
	this->invoke_compute_shader();

	return true;
}

void invoke_compute_shader(void)
{
	ID3D11ShaderResourceView *nullSRV = { NULL };
	ID3D11UnorderedAccessView *nullUAV = { NULL };
	ID3D11ComputeShader *nullCShader = { NULL };

	this->renderer->GetDeviceContext()->CSSetShader(this->shader, NULL, 0);

	this->renderer->GetDeviceContext()->CSSetUnorderedAccessViews(1, 1, &this->texture_uav, NULL);

	this->renderer->GetDeviceContext()->Dispatch(32, 32, 1);

	this->renderer->GetDeviceContext()->CSSetShaderResources(0, 1, &nullSRV);
	this->renderer->GetDeviceContext()->CSSetUnorderedAccessViews(0, 1, &nullUAV, 0);
	this->renderer->GetDeviceContext()->CSSetShader(nullCShader, 0, 0);
}


Here is the compute shader code itself:

RWTexture2D<float4> output_texture : register(u0);

[numthreads(32, 32, 1)]
void CSMain( uint3 dispatch_tid : SV_DispatchThreadID )
{
	uint2 index = uint2(dispatch_tid.x, dispatch_tid.y);;
	output_texture[index] = float4(1.0f, 1.0f, 0.0f, 1.0f);
}

I've taken a look at the texture in Visual Studio's Resource Visualizer and it shows an empty texture.  What are my doing wrong?

Share this post


Link to post
Share on other sites
MJP    19787

When you call CSSetUnorderedAccessViews to bind your texture to the CS stage, you're passing "1" as the StartSlot parameter. This is binding your UAV to register u1, and not u0 like your shader expects. Try changing that to 0 instead.

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