[Maya] [CGFX Shaders] Using a Texture Sampler in a Vertex Fragment

I am using a Texture to offset Vertex Positions in a Vertex Fragment program. After much pulling out of hair, I learned the hard way that the first texture sampled in the Pixel Fragment Shader overrides/replaces the Texture Sampled in the Vertex Fragment Program!

Say I got two Textures, Diffuse and Offset.
In the VP, I lookup the Offset Texture and move the position.
IN the FP, I lookup the Diffuse color and render the object.
In Maya, the Diffuse Map is used both for Offset and Color!

If i go back to the FP, access the Offset map before the Diffuse map, then it works as expected!

Has anyone else come across this before? Is this a known issue? A limitation? A bug?

Hi Robert,

Can you send me a simple cgfx file demonstrating the problem?
kees.rijnen at autodesk

thank you.

float4x4 WorldViewProjection   : WorldViewProjection;

string description = "Shader for testing Images in a Vertex Shader.";

texture2D Diffuse;
texture2D Offset;

sampler2D DiffuseSampler
<
    string UIName = "Diffuse Map";
> = sampler_state
{
    Texture       = <Diffuse>;
    MinFilter     = LinearMipMapLinear;
    MagFilter     = Linear;
    WrapS         = Wrap;
    WrapT         = Wrap;
};

sampler2D OffsetSampler
<
    string UIName = "Offset Map";
> = sampler_state
{
    Texture       = <Offset>;
    MinFilter     = LinearMipMapLinear;
    MagFilter     = Linear;
    WrapS         = Wrap;
    WrapT         = Wrap;
};

float positionOffset
<
    string  UIWidget = "slider";
    string  UIName = "Offset";
    float   UIMin = 0.0f;
    float   UIMax = 10.0f;
    float   UIStep = 0.1f;
> = 1.0f;

struct appData
{
    float4 position    : POSITION;
    float2 uv0         : TEXCOORD0;
    float4 normal      : NORMAL;
    float4 vertexcolor : COLOR0;
};

struct VS_OUTPUT
{
    float4 position      : POSITION;
    float2 uv0           : TEXCOORD0;
    float4 vertexcolor   : COLOR0;
};

VS_OUTPUT VS_1tex(appData IN)
{
    VS_OUTPUT OUT;
    float4 offsetTexture = tex2D(OffsetSampler, IN.uv0);
    float offset = offsetTexture.b * positionOffset;
    float4 pos = float4((IN.position.xyz + (IN.normal.xyz * offset)), IN.position.w);
    OUT.position = mul(WorldViewProjection, pos);
    OUT.uv0 = IN.uv0;
    OUT.vertexcolor = IN.vertexcolor;
    return OUT;
}

float4 PS_template(VS_OUTPUT IN): COLOR
{
    // declaring the offset Map before the Diffuse Map and ensuring it doesn't
    // get compiled out will make it work correctly in the VS.
    //float4 offsetMap = tex2D(OffsetSampler, IN.uv0);
    float4 diffuse = tex2D(DiffuseSampler, IN.uv0);
    
    //diffuse *= offsetMap;
    return diffuse;
}

technique vertexTests
{
    pass P0
    {
        VertexShader = compile vp40 VS_1tex();
        FragmentProgram = compile fp40 PS_template();
    }
}

Here is a stripped down shader i used to isolate the issue. Uncomment the two lines in the PS to see what happens.
Thanks!