• Advertisement
Sign in to follow this  

Compute Shader won't Fill Texture

This topic is 1088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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
Sign in to follow this  

  • Advertisement