I'm trying to get a very basic Compute Shader to write to a Texture2D and render its result to screen. so far I have written this code to bind UAV to Texture2D and pass that to my Compute Shader. but at the end all I get is a black screen.
I don't have any errors or warnings in debug layer.
here's my compute shader.
struct CSInput
{
uint3 dispatchThreadId : SV_DispatchThreadID;
};
RWTexture2D<float4> screenTexture;
[numthreads(32, 32, 1)]
void main(CSInput input)
{
input.screenTexture[input.dispatchThreadId.xy] = float4(0.5f, 0.2f, 0.8f, 1.0f);
}
this is my function for Texture2D.
D3D11_TEXTURE2D_DESC textureDesc{};
textureDesc.Width = this->width;
textureDesc.Height = this->height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
hr = this->device->CreateTexture2D(&textureDesc, nullptr, this->screenTexture.GetAddressOf());
this is my function for UAV.
D3D11_UNORDERED_ACCESS_VIEW_DESC descUAV{};
descUAV.Format = DXGI_FORMAT_UNKNOWN;
descUAV.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
descUAV.Texture2D.MipSlice = 0;
hr = this->device->CreateUnorderedAccessView(this->screenTexture.Get(), &descUAV, this->UAV.GetAddressOf());
and this is how I create my Target View.
D3D11_RENDER_TARGET_VIEW_DESC descRTV{};
descRTV.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
descRTV.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
hr = this->device->CreateRenderTargetView(this->screenTexture.Get(), &descRTV, this->RTV.GetAddressOf());
in my main loop I bind and unbind UAV to keep it ready for write and read. here's the code.
// unbinding and making screen texture ready for writing
ID3D11RenderTargetView* nullRTV = nullptr;
this->deviceContext->OMSetRenderTargets(1, &nullRTV, nullptr);
// dispatching compute shader and writing to screen
this->deviceContext->CSSetUnorderedAccessViews(0, 1, this->UAV.GetAddressOf(), nullptr);
this->deviceContext->CSSetShader(this->computeShader.Get(), nullptr, 0);
this->deviceContext->Dispatch(this->width / 32, this->height / 32, 1);
// unbinding screen texture and making it ready for reading
ID3D11UnorderedAccessView* nullUAV = nullptr;
this->deviceContext->CSSetUnorderedAccessViews(0, 1, &nullUAV, nullptr);
// showing screen texture
this->deviceContext->OMSetRenderTargets(1, this->RTV.GetAddressOf(), nullptr);
this->swapChain->Present(1, 0);
thank you in advance
edit: I found a bug in my Compute Shader and fixed it, but result is still the same. the code is updated in the post.