Sign in to follow this  
pnt1614

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this