Jump to content
  • Advertisement
Sign in to follow this  
pnt1614

How to update a matrix array for shader in DirectX 11?

This topic is 1996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to update a matrix array, then send to GPU but it is not working. I tried 2 ways, first I updated a matrix array in a constant buffer using map/unmap. The second method is using SetMatrixArray() in an effect.

 

The first method source code.

    HRESULT hr;
    D3D11_MAPPED_SUBRESOURCE MappedResource;
    V( pd3dImmediateContext->Map( g_pcbVSPerFrame11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
    CB_VS_PER_FRAME* pVSPerFrame = ( CB_VS_PER_FRAME* )MappedResource.pData;

	XMMATRIX M1 = XMMatrixTranslation(30.0f, 2.0f, 10.0f);
	XMMATRIX M2 = XMMatrixIdentity();	
	XMMATRIX M3 = XMMatrixIdentity();

	pVSPerFrame->g_mMatrixArray[0] = M1;
	pVSPerFrame->g_mMatrixArray[1] = M2;
	pVSPerFrame->g_mMatrixArray[2] = M3;

    pd3dImmediateContext->Unmap( g_pcbVSPerFrame11, 0 );
    pd3dImmediateContext->VSSetConstantBuffers( 1, 1, &g_pcbVSPerFrame11 );		

 

Shader:

VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;
    
	float4 temp = 0;
	float3 posL = float3(1.0f, 0.0f, 0.0f);
	temp = mul(float4(input.Position, 1.0f), g_mMatrixArray[1]);
	//temp = mul(float4(posL, 1.0f), g_mMatrixArray[1]);
	//temp = mul(float4(posL, 1.0f), g_mMatrixArray[2]);

    // Transform the position from object space to homogeneous projection space
    Output.Position = mul( float4(temp.xyz, 1.0f), g_mWorldViewProjection );
    
    // Just copy the texture coordinate through
    Output.Color = input.Color; 
    
    return Output;    
}

 

g_mMatrixArray[0], g_mMatrixArray[1] and g_mMatrixArray[2] are the same values (1.0f, 0.0f, 0.0f)

 

 

The second method source code:


        XMMATRIX M1 = XMMatrixIdentity();
	XMMATRIX M2 = XMMatrixTranslation(2.0f, 2.0f, 0.0f);
	XMMATRIX M3 = XMMatrixIdentity();

	g_mMatrixArray.resize(3);

	XMFLOAT4X4* M = &g_mMatrixArray[0];
	g_pMatrixArray->SetMatrixArray(reinterpret_cast<const float*>(&g_mMatrixArray[0]), 0, g_mMatrixArray.size());

 

 

In shader:


cbuffer cbPerFrame : register( b1 )
{
    float4x4 g_mBoneTransform[96];
};

VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;

	float4 temp = 0;
	float3 posL = float3(1.0f, 0.0f, 0.0f);
	temp = mul(float4(posL, 1.0f), g_mBoneTransform[0]);
		
    // Transform the position from object space to homogeneous projection space
	Output.Position = mul( float4(temp.xyz, 1.0f), g_mWorldViewProjection );    
       
    // Just copy the texture coordinate through
    Output.TextureUV = input.Color;
    
    return Output;    
}

 

 

Both of methods do not work sad.png. Anybody helps me, please?

Edited by steven166

Share this post


Link to post
Share on other sites
Advertisement

First method is correct way.

 

I believe you cannot see any change because in your shader you have g_mMatrixArray[1] instead of g_mMatrixArray[0].

Share this post


Link to post
Share on other sites

Thank Zaoshi Kaba, but I even modify the shader for debug, but values of three arrays are the same (1, 0, 0)

 

cbuffer cbPerFrame: register(b1)
{
float4x4 g_mMatrixArray[10];
}

VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;    

    float4 temp = 0;
    float3 posL = float3(1.0f, 0.0f, 0.0f);
    float4x4 temp1 = g_mMatrixArray[0] + g_mMatrixArray[1] + g_mMatrixArray[2];
    temp = mul(float4(posL, 1.0f), temp1);

    // Transform the position from object space to homogeneous projection space
    Output.Position = mul( float4(temp.xyz, 1.0f), g_mWorldViewProjection );
    
    Output.Color = input.Color;

    return Output;
}

 

errorls.jpg

 

As you can see the image, all matrices have the same value and they are float1x4 instead of float4x4. I do not understand sad.png

Edited by steven166

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!