DGSL effect texture bug

Dec 18, 2013 at 11:09 AM
I tried for some time to get normal mapping working with DGSL effects, but there is a bug with the texture loading code for DGSL effects, so I hope to save some time for others. The bug is in the CMO loading code for DGSL effects. The following snippet is from DirectX::Model::CreateFromCMO.
// Create Effects
for( UINT j = 0; j < *nMats; ++j )
{
    auto& m = materials[ j ];

    if ( fxFactoryDGSL )
    {
        DGSLEffectFactory::DGSLEffectInfo info;
        info.name = m.name.c_str();
        info.specularPower = m.pMaterial->SpecularPower;
        info.perVertexColor = true;
        info.enableSkinning = enableSkinning;
        info.alpha = m.pMaterial->Diffuse.w;
        info.ambientColor = XMFLOAT3( m.pMaterial->Ambient.x, m.pMaterial->Ambient.y, m.pMaterial->Ambient.z );
        info.diffuseColor = XMFLOAT3( m.pMaterial->Diffuse.x, m.pMaterial->Diffuse.y, m.pMaterial->Diffuse.z );
        info.specularColor = XMFLOAT3( m.pMaterial->Specular.x, m.pMaterial->Specular.y, m.pMaterial->Specular.z );
        info.emissiveColor = XMFLOAT3( m.pMaterial->Emissive.x, m.pMaterial->Emissive.y, m.pMaterial->Emissive.z );
        info.texture = m.texture[0].c_str();
        info.pixelShader = m.pixelShader.c_str();
        
        for( int i = 0; i < 7; ++i )
        {
            info.textures[i] = m.texture[ i+1 ].empty() ? nullptr : m.texture[i].c_str();
        }

        m.effect = fxFactoryDGSL->CreateDGSLEffect( info, nullptr );

        auto dgslEffect = reinterpret_cast<DGSLEffect*>( m.effect.get() );
        dgslEffect->SetUVTransform( XMLoadFloat4x4( &m.pMaterial->UVTransform ) );
    }
The for loop copying texture names into info.textures[] is incorrect, it should be:
        for( int i = 0; i < 7; ++i )
        {
            info.textures[i] = m.texture[ i+1 ].empty() ? nullptr : m.texture[ i+1 ].c_str();
        }
As it stands, loading a CMO file with a DGSL effect factory will cause the resulting effect to have the first texture duplicated, so your second texture will end up being third, etc. Obviously my DGSL shader was expecting the normal map to be in the second texture slot, but since the diffuse texture was duplicated to that slot, it was attempting to use the diffuse texture as the normal map.
Coordinator
Dec 18, 2013 at 4:55 PM
Edited Dec 18, 2013 at 4:59 PM
Thanks for the report...

Fixed with changeset 32625
Marked as answer by walbourn on 12/23/2013 at 11:06 PM