Jump to content
  • Advertisement
Sign in to follow this  
jdub

Compute shader doesn't produce result..

This topic is 1781 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 have a compute shader which simply writes a value to a texture.  The result is copied from the unordered access texture to a texture which is bound as an SRV and rendered as a quad.  However, the screen is blank.  According to the Visual Studio 2012 Resource Visualizer in the Graphics debugger, both the UAV texture and the output texture are empty.

 

Here is the code which creates the compute shader as well as the UAV and a texture to copy the output to:

bool NBodySim::createComputeShader(LPCWSTR path)
	{
		HRESULT result = 0;
		LPCSTR target = "cs_5_0";
		DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
		ID3DBlob* blob = NULL;
		ID3DBlob* errorMessageBlob = NULL;

	#ifdef _DEBUG
		shaderFlags |= D3DCOMPILE_DEBUG;
	#endif

		result = D3DCompileFromFile(path, NULL, D3D_COMPILE_STANDARD_FILE_INCLUDE,  "main", target, shaderFlags, 0, &blob, &errorMessageBlob);
		if(FAILED(result))
		{
			if(errorMessageBlob)
				OutputDebugStringA((LPCSTR)errorMessageBlob->GetBufferPointer()); 
			
			SafeRelease<ID3DBlob>(&blob);
			SafeRelease<ID3DBlob>(&errorMessageBlob);

			return false;
		}
	
		result = this->device->CreateComputeShader(
					blob->GetBufferPointer(), 
					blob->GetBufferSize(),
					NULL, 
					&this->cShader);
		if(FAILED(result))
			return false;

		//create a texture for the uav
		D3D11_TEXTURE2D_DESC uavTextureDesc;
		ZeroMemory(&uavTextureDesc, sizeof(D3D11_TEXTURE2D_DESC));
		uavTextureDesc.ArraySize = 1;
		uavTextureDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
		uavTextureDesc.CPUAccessFlags = 0;
		uavTextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
		uavTextureDesc.Height = 256;
		uavTextureDesc.Width = 256;
		uavTextureDesc.MipLevels = 1;
		uavTextureDesc.MiscFlags = 0;
		uavTextureDesc.Usage = D3D11_USAGE_DEFAULT;
		uavTextureDesc.SampleDesc.Count = 1;
		uavTextureDesc.SampleDesc.Quality = 0;

		if(FAILED(this->device->CreateTexture2D(&uavTextureDesc, NULL, &this->uavTexture)))
			return false;

		//create unordered access view
		D3D11_UNORDERED_ACCESS_VIEW_DESC descView;
		ZeroMemory( &descView, sizeof(descView) );
		descView.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
		descView.Buffer.FirstElement = 0;

		descView.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;  
		descView.Texture2D.MipSlice = 0;
		
		if(FAILED(this->device->CreateUnorderedAccessView( this->uavTexture, &descView, &this->uav )))
			return false;

		//create output texture/srv for Compute Shader
		D3D11_TEXTURE2D_DESC csOutputTextureDesc;
		ZeroMemory(&csOutputTextureDesc, sizeof(D3D11_TEXTURE2D_DESC));
		csOutputTextureDesc.ArraySize = 1;
		csOutputTextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
		csOutputTextureDesc.CPUAccessFlags = 0;
		csOutputTextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
		csOutputTextureDesc.Height = 256;
		csOutputTextureDesc.Width = 256;
		csOutputTextureDesc.MipLevels = 1;
		csOutputTextureDesc.MiscFlags = 0;
		csOutputTextureDesc.Usage = D3D11_USAGE_DEFAULT;
		csOutputTextureDesc.SampleDesc.Count = 1;
		csOutputTextureDesc.SampleDesc.Quality = 0;

		if(FAILED(this->device->CreateTexture2D(&csOutputTextureDesc, NULL, &this->csOutputTexture)))
			return false;

		D3D11_SHADER_RESOURCE_VIEW_DESC csOutputTextureSRVDesc;
		ZeroMemory(&csOutputTextureSRVDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
		csOutputTextureSRVDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
		csOutputTextureSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
		csOutputTextureSRVDesc.Texture2D.MipLevels = 1;
		csOutputTextureSRVDesc.Texture2D.MostDetailedMip = 0;

		if(FAILED(this->device->CreateShaderResourceView(this->csOutputTexture, &csOutputTextureSRVDesc, &this->csOutputTextureSRV)))
			return false;

		return true;
	}

Here is the code where the compute shader is run:

this->context->CSSetShader(this->cShader, NULL, 0);

		ID3D11UnorderedAccessView* uavs = {this->uav};
		this->context->CSSetUnorderedAccessViews(0, 1, &uavs, NULL);
		this->context->Dispatch(16,16,1);

		this->context->CSSetShader(NULL, NULL, 0);

		this->context->CopyResource(this->uavTexture, this->csOutputTexture);

Here is the actual code for the compute shader:

RWTexture2D<float4> output;

[numthreads(16, 16, 1)]
void main( uint3 threadID : SV_DispatchThreadID )
{
	output[threadID.xy] = float4(1.0f,0.0f,0.0f, 0.5f);
}

Any ideas?

Edited by jdub

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!