I use a uint texture to store material ID's.
The shader output struct I use for this is and the ID constant:
uint matID;
struct PixelShaderOutput
{
uint RenderMaterialID : SV_Target0;
};
Write to the output targets:
PixelShaderOutput PShader(PixelShaderInput input)
{
PixelShaderOutput output = (PixelShaderOutput) 0;
output.RenderMaterialID = matID;
return output;
}
Then just read back the ID texture at a later stage in your render pipeline
Texture2D<uint> RenderMaterialIDs;
uint MatID = RenderMaterialIDs.Load(uint3(globalIdx.x, globalIdx.y, 0));