# XNA hlsl tex2D() only reads 3 channels from normal maps and specular maps

Our engine uses deferred rendering and at the main draw phase gathers plenty of data from the objects it draws.

In order to save on tex2D calls, we packed our objects' specular maps with all sorts of data, so three out of four channels are already taken. To make it clear: I am talking about the assets that come with the models and are stored in their material's Specular Level channel, not about the RenderTarget. So now I need another information to be stored in the alpha channel, but I cannot make the shader to read it properly! Nomatter what I write into alpha it ends up being 1 (255)! I tried:

1. saving the textures in PNG/TGA formats.

2. turning off pre-computed alpha in model's properties.

Out of every texture available to me (we use Diffuse map, Normal Map and Specular Map) I was only able to read alpha successfully from the Diffuse Map!

Here is how I add specular and normal maps to my model's material in the content processor:

if (geometry.Material.Textures.ContainsKey(normalMapKey))
{
ExternalReference<TextureContent> texRef = geometry.Material.Textures[normalMapKey];
geometry.Material.Textures.Remove("NormalMap");
}

...

foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture
in material.Textures)
{
if ((texture.Key == "Texture") ||
(texture.Key == "NormalMap") ||
(texture.Key == "SpecularMap"))
}


In the shader I obviously use:

float4 data = tex2D(specularMapSampler, TexCoords);


so data.a is always 1 in my case, could you suggest a reason?

• I've found that a lot of applications do funky things with alpha when saving (e.g. if alpha is 0 colour data is discarded for that pixel). Are you absolutely certain the data being saved into your files is what you think it is? Oct 29 '13 at 17:55
• I have no idea how to make sure it does other than open the texture's channels in Photoshop. The latter seems to show alpha stored the way I intended it to. I did other alpha manipulations with the channel map and they all worked noticeably. I know that in png alpha is stored in a peculiar way and that XNA 4 premultiplies alphas but I accounted for these in my experiments. I also mentioned that I managed to successfully write and read alpha from the main model's texture (the diffuse channel) so I really doubt the problem is in the way I write textures. Oct 30 '13 at 7:56

Ok I returned to this question now as our engine started requiring more channels and finally I solved it. I am using a custom content processor, and in such a case the user should manually specify whether he needs premultiplied alpha in the content processor. Or so I concluded :). Basically I added this code into my ConvertMaterial() method:

OpaqueDataDictionary processorParameters = new OpaqueDataDictionary();
processorParameters["ColorKeyColor"] = this.ColorKeyColor;
processorParameters["ColorKeyEnabled"] = false;
processorParameters["TextureFormat"] = this.TextureFormat;
processorParameters["GenerateMipmaps"] = this.GenerateMipmaps;
processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo;
processorParameters["PremultiplyTextureAlpha"] = false;
processorParameters["ColorKeyEnabled"] = false;