   Texture Atlas Pixel Shader -- where am I going wrong?
  First, some context as to the underlying shader code:float4x4 wvp;float scale;texture atlasMap;sampler atlasSampler = sampler_state { texture = <atlasMap> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = wrap; AddressV = wrap;};struct vData{    float4 Position : POSITION0;    float2 Coords : TEXCOORD0;    float Index : COLOR0;};struct pData{    float4 Position : POSITION0;    float2 Coords : TEXCOORD0;    float4x4 Map : COLOR0;};pData vShader(vData input){    pData output = (pData)0;    output.Position = mul(input.Position, wvp);    output.Coords = input.Coords;    float4x4 bMap = -0.48f;   int col = floor(input.Index*0.25f);    int row = floor(input.Index-4*col);    if (row == 0) {if (col == 0) { bMap._m00 = 1; }if (col == 1) { bMap._m01 = 1; }if (col == 2) { bMap._m02 = 1; }if (col == 3) { bMap._m03 = 1; }}if (row == 1 ) {if (col == 0) { bMap._m10 = 1; }if (col == 1) { bMap._m11 = 1; }if (col == 2) { bMap._m12 = 1; }if (col == 3) { bMap._m13 = 1; }} if (row == 2 ) {if (col == 0) { bMap._m20 = 1; }if (col == 1) { bMap._m21 = 1; }if (col == 2) { bMap._m22 = 1; }if (col == 3) { bMap._m23 = 1; }} if (row == 3 ) {if (col == 0) { bMap._m30 = 1; }if (col == 1) { bMap._m31 = 1; }if (col == 2) { bMap._m32 = 1; }if (col == 3) { bMap._m33 = 1; }}output.Map = bMap;    return output;}float4 pShader(pData input) : COLOR0{    float4 answer = 0;float3 vertWeights = 0;float3x4 vertColors = 0;//Texture atlas coordinates need constraintsfloat2 uvin = frac(input.Coords*scale)*0.25f;int trow = 0;int im = 0;int jm = 0;float sum = 0;//Look up those textures that have positive blends// and sample them.  Ignore the others.while (trow<3 && jm<4) {float blend = input.Map[im][jm];if (blend>0) {float2 uvtile = (im*0.25f, jm*0.25f);if (trow==0) { vertWeights.x = blend;vertColors[0] = tex2D(atlasSampler, uvin+uvtile); }if (trow==1) { vertWeights.y = blend; vertColors[1] = tex2D(atlasSampler, uvin+uvtile); }if (trow==2) { vertWeights.z = blend; vertColors[2] = tex2D(atlasSampler, uvin+uvtile); }sum += blend;trow++;}im++;if (im>3) {im = 0;jm++; }}//Insure the blends sum to 1vertWeights /= sum;//Now blend and return the answeranswer = mul(vertWeights, vertColors);return answer;}technique ColorMap{    pass Pass1    {   VertexShader = compile vs_3_0 vShader();   PixelShader = compile ps_3_0 pShader();    }}The vertex shader is supposed to take an index to a 4x4 texture atlas and set a particular value in a blend weight matrix to 1 where the others are at -0.48.  This way each triangle will have at most three weights >= 0.  Note that I cannot simply have bMap[row][col]=1.  Setting values in matrices via indirect variables is simply not allowed, at least not now.Within the pixel shader I scan through the now interpolated blend weights for positive values.  VertWeights contains each positive blend and the vertColors matrix stores the corresponding sampled texture.  Note that I track the blend sum for normalization later.  Unused blend weights have a 0 value and are conveniently excluded.  Again vertColors[trow] = tex2D(atlasSampler, uvin+uvtile); returns an error.Most texture splatting I have seen calculates texture sample weights individually and adds them one by one.  I fill a floatnx4 matrix with n sampled colors in each row, then mul it with the blend weight vector.  In my case n=3.  Both methods work, but I prefer this simpler approach to save instructions if possible.Now to my problem:  If my texture atlas is configured likeabcdefghijklmnopthen the only images I get to see are along the main diagonal (a,f,k,p), regardless of the actual index I pass in.  My test code allows me to increment the indices by one at a time.  I have noticed a pattern to this: 4 textures in a row followed by the next texture in the "sequence".  If someone can identify the issue with the shader I would really appreciate it.   
  This line,float2 uvtile = (im*0.25f, jm*0.25f);turned out to be the problem.  The uvtile parameter ended up being (jm*0.25f, jm*0.25f), limiting access to the atlas.  float2 needs to be explicitly cast:float2 uvtile = float2(im*0.25f, jm*0.25f);Now there is only one small, cosmetic, issue: vertical and horizontal lines in the map which I suspect are caused by blending errors.  I have seen others dealing with similar problems but any feedback on cleaning them up would be welcome.Remember that this is a 4x4 texture atlas (2Kx2K).  The topic referenced athttp://www.gamedev.net/community/forums/topic.asp?topic_id=438831handles a 2x2 atlas (512x512).  Any proposed solution needs to be applicable to any atlas scale and division.;  Assuming you're dealing with seamless textures, the way I dealt with it in Ballerium was to have the artists duplicate the top and bottom pixels above and to the left (IIRC 2 pixels, but maybe 4), then use the middle of the tile instead of the full tile as the texture (for example for 128x128, use 126x126, or 124x124 if 4 pixels are copied). That was in pre-shader days, and worked very well.   