I am trying to implement object picking via a shaders. My intent is to create a texture2d that I would write out ID values describing individual objects.
Following drawing the objects, I would query back the pixel value at the focus point and read out the ID of the object.
I've created my texture2d as follows:
// Create a very tiny selection texture.
// This will record the color ID as unique identifier of a selectable object.
D3D11_TEXTURE2D_DESC desc;
desc.Width = 1;
desc.Height = 1;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R32_UINT; // 16 bit integer, hold up to 65536 possible unique IDs.
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_STAGING; // We will write via GPU and read to CPU!
desc.BindFlags = D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
HRESULT result = m_deviceResources->GetD3DDevice()->CreateTexture2D(&desc, 0, &m_colorRenderTarget);
if (FAILED(result)) {
OutputDebugString(L"PickRenderer: Unable to create Texture2D");
}
Is it possible to then to make a pixel shader that would write out an R32 integer value, rather than the float values for RGBA? If so, how would I best do this?
My pixel shader currently writes float RGB values out as follows:
// A constant buffer that stores the model transform.
cbuffer ModelConstantBuffer : register(b0)
{
float4x4 modelToWorld;
float3 color; // Instead use unsigned int32 here if possible?
};
// Per-vertex data used as input to the vertex shader.
struct PixelShaderInput
{
min16float4 pos : POSITION;
};
// Can this use a different return value for uint?
min16float4 main(PixelShaderInput IN) : SV_TARGET
{
return min16float4(color, 1.f);
}
Thanks, I've been trying to find an answer to this, but it seems very unusual to write out non-float values if it is possible.