Matrix post-shader multiplication explosion

Started by
3 comments, last by joeblack 11 years, 1 month ago

//////////////////////////////////////////////////////////////////////////

//                    Character Animation with Direct3D                    //

//                           Author: C. Granberg                            //

//                               2008 - 2009                                //

//////////////////////////////////////////////////////////////////////////



//Transformation Matrices

matrix matW;

matrix matVP;



//World Light Position

float3 lightPos;

float4 materialColor;



//Texture

texture texDiffuse;



//Sampler

sampler DiffuseSampler = sampler_state

{

   Texture = (texDiffuse);

   MinFilter = Linear;   MagFilter = Linear;   MipFilter = Linear;

   AddressU  = Wrap;     AddressV  = Wrap;     AddressW  = Wrap;

   MaxAnisotropy = 16;

};



//Vertex Input

struct VS_INPUT

{

     float4 position : POSITION0;

     float3 normal   : NORMAL;

     float2 tex0     : TEXCOORD0;

};



//Vertex Output / Pixel Shader Input

struct VS_OUTPUT

{

     float4 position : POSITION0;

     float2 tex0     : TEXCOORD0;

     float  shade     : TEXCOORD1;

};



//Vertex Shader

VS_OUTPUT vs_lighting(VS_INPUT IN)

{

    VS_OUTPUT OUT = (VS_OUTPUT)0;



    //getting the position of the vertex in the world

    float4 posWorld = mul(IN.position, matW);

    float4 normal = normalize(mul(IN.normal, matW));

    

    //getting to position to object space

    OUT.position = mul(posWorld, matVP);

    

    OUT.shade = max(dot(normal, normalize(lightPos - posWorld)), 0.2f);

    

    OUT.tex0 = IN.tex0;

    

    return OUT;

}



//Pixel Shader

float4 ps_lighting(VS_OUTPUT IN) : COLOR0

{

    float4 color = tex2D(DiffuseSampler, IN.tex0);

    return IN.shade * materialColor;

}



//Lighting Technique

technique Lighting

{

    pass P0

    {        

        Lighting = false;

        

        VertexShader = compile vs_2_0 vs_lighting();

        PixelShader  = compile ps_2_0 ps_lighting();

    }

}



//Pixel Shader

float4 ps_shadow(VS_OUTPUT IN) : COLOR0

{

    return float4(0.3f, 0.3f, 0.3f, 1.0f);

}



//Shadow Technique

technique Shadow

{

    pass P0

    {        

        Lighting = false;

        

        VertexShader = compile vs_2_0 vs_lighting();

        PixelShader  = compile ps_2_0 ps_shadow();

    }

}



////////////////////////////////////////////////////////////////////////////



extern float4x4 FinalTransforms[100];

extern int NumVertInfluences = 2; // <--- Normally set dynamically.



//Vertex Input

struct VS_INPUT_SKIN

{

     float4 position : POSITION0;

     float3 normal   : NORMAL;

     float2 tex0     : TEXCOORD0;

     float4 weights  : BLENDWEIGHT0;

     int4   boneIndices : BLENDINDICES0;

};



VS_OUTPUT vs_Skinning(VS_INPUT_SKIN IN)

{

    VS_OUTPUT OUT = (VS_OUTPUT)0;



    float4 p = float4(0.0f, 0.0f, 0.0f, 1.0f);

    float3 norm = float3(0.0f, 0.0f, 0.0f);

    float lastWeight = 0.0f;

    int n = NumVertInfluences-1;

    

    IN.normal = normalize(IN.normal);

    

    

     

     

    for(int i = 0; i < n; ++i)

    {

        lastWeight += IN.weights;

        p += IN.weights * mul(IN.position, FinalTransforms[IN.boneIndices]);

    }

    lastWeight = 1.0f - lastWeight;

    

    p += lastWeight * mul(IN.position, FinalTransforms[IN.boneIndices[n]]);

    

    p.w = 1.0f;

    

    p.w = 1.0f;        

    float4 posWorld = mul(p, matW);

    OUT.position = mul(posWorld, matVP);

    OUT.tex0 = IN.tex0;

    

    //Calculate Lighting

    norm = normalize(norm);

    norm = mul(norm, matW);

    OUT.shade = max(dot(norm, normalize(lightPos - posWorld)), 0.2f);

     

    return OUT;

}



technique Skinning

{

    pass P0

    {

        Lighting = false;

        

        VertexShader = compile vs_2_0 vs_Skinning();

        PixelShader  = compile ps_2_0 ps_lighting();        

    }

}


if (boneMesh->pSkinInfo != NULL)

        {        

            // set up bone transforms

            int numBones = boneMesh->pSkinInfo->GetNumBones();

            for(int i=0;i < numBones;i++)

            {

                D3DXMatrixMultiply(&boneMesh->currentBoneMatrices,

                                    &boneMesh->pBoneOffsetMatrices,

                                    boneMesh->ppBoneMatrixPtrs);

            }



            D3DXMATRIX view, proj, identity;                

            g_pEffect->SetMatrixArray("FinalTransforms", &boneMesh->currentBoneMatrices[0], boneMesh->pSkinInfo->GetNumBones());

            //D3DXMatrixIdentity(&identity);



            //Render the mesh

            for(int i=0;i < (int)boneMesh->NumAttributeGroups;i++)

            {

                int mtrlIndex = boneMesh->pAttributeTable.AttribId;

                g_pDevice->SetMaterial(&(boneMesh->materials[mtrlIndex]));

                g_pDevice->SetTexture(0, boneMesh->textures[mtrlIndex]);

                g_pEffect->SetMatrix("matW", &bone->CombinedTransformationMatrix);

                g_pEffect->SetVector( "materialColor",

                                         ( D3DXVECTOR4* )&(

                                         boneMesh->materials.Diffuse ) );



                g_pEffect->SetTexture("texDiffuse", boneMesh->textures[mtrlIndex]);

                D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Skinning");

                g_pEffect->SetTechnique(hTech);

                g_pEffect->Begin(NULL, NULL);

                g_pEffect->BeginPass(0);



                boneMesh->pOrigMesh->DrawSubset(mtrlIndex);



                g_pEffect->EndPass();

                g_pEffect->End();

            }

 

Is this the model's problem? However, how can I shrink/scale it down? I have to do this myself and it's a pre-rigged model.

You also seeing that the bottom part of the character is clipped off. Why is that?

I don't know yet because the view and projection matrices seem to be correct as other models are displayed correctly, apart from skinned ones.

Thanks

Jack

Advertisement
No it's not clipped look it's got feet you are looking down at it

Hi, I am on another task now. This is memory leaks. I've got 3 memory leaks.
I've heard of other people who used PIX successfully finding out memory leaks

http://creyavaichxxi.awardspace.com/wordpress/?p=218

But in my case, the reported size from the IDE and PIX don't match, I have no clue what unallocated index buffers and vertex buffers are as PIX indicate me.
No ideas what all the magic numbers mean. I looked google and there is only a little information and I don't quite understand.
Could anyone explain a little bit how I can make use of PIX to track down memory leaks?

This drives me crazy...
D3DX: MEMORY LEAKS DETECTED: 3 allocations unfreed (28024 bytes)
D3DX: Set HKLM\Software\Microsoft\Direct3D\D3DXBreakOnAllocId=0x5f5c to debug

I've tried to create a key in the registry, but the call stack went to some mysterious place, like somewhere in QT


Thanks
Jack

One slow but always working method is to disable one feature at a time until the leak stops. For example, start your engine without loading any models to see if it is connected to the model.

But in my case, the reported size from the IDE and PIX don't match,

Hi,

in my case pix also reported strange things. It looks to me, when you forget to release one thing, PIX will report also lot of other "unreleases" that aren't relevant. E.g. : once i forget to release one texture, but PIX showed me around 10 unreleased things, including index and vertex buffers. After releasing of texture, all other leaks dissapeared.

So i woudn't believe PIX in everything it's complaining.

This topic is closed to new replies.

Advertisement