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 GBufferPixelShaderOutputPixelShaderOutput
{
float4 Color : SV_Target0;
uint RenderMaterialID : SV_Target2;SV_Target0;
};
Write to the output targets:
GBufferPixelShaderOutputPixelShaderOutput PShader(PixelShaderInput input)
{
GBufferPixelShaderOutputPixelShaderOutput output = (GBufferPixelShaderOutputPixelShaderOutput) 0;
float4 Color = colorMap.Sample(colorSampler, input.TexCoord);
output.RenderMaterialID = matID;
output.Color = Color;
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));