For example, in deferred renderer, in pixel shader i would like to write distance from camera to pixel (read from g-buffer R32F texture) at current cursor position.
What kind of buffers i could use (if any, except render targets) and how to setup those to be able to access that value on CPU side?
With google i couldn't find any example code, and i don't know with what keywords to search properly.
I tried to write some code, but i am stuck as don't know what is correct "semantic" for shader code side:
c++
struct MyStruct
{
vec4 myData;
};
...
std::memset(&buffDesc, 0, sizeof(buffDesc));
buffDesc.Usage = D3D11_USAGE_DEFAULT;
buffDesc.ByteWidth = sizeof(MyStruct);
buffDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
buffDesc.CPUAccessFlags = 0;
hr = dev->CreateBuffer(&buffDesc, nullptr, &m_writeBuffer);
if (FAILED(hr))
{
...
}
std::memset(&buffDesc, 0, sizeof(buffDesc));
buffDesc.Usage = D3D11_USAGE_STAGING;
buffDesc.ByteWidth = sizeof(MyStruct);
buffDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
buffDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
hr = dev->CreateBuffer(&buffDesc, nullptr, &m_readBuffer);
if (FAILED(hr))
{
...
}
...
//after drawing
Context->CopyResource(m_readBuffer, m_writeBuffer);
MyStruct s;
D3D11_MAPPED_SUBRESOURCE ms;
Context->Map(m_readBuffer, 0, D3D11_MAP_READ, 0, &ms);
s.myData = *(vec4*)ms.pData;
Context->Unmap(m_readBuffer, 0);
... // use myData
hlsl
??? MyStruct : register(???)
{
float4 myData;
};
void PixelShader(...)
{
...
myData = something;
...
}
↧