locked
Simple HLSH question

    Question

  • in HLSH I have a struct like that

    cbuffer SimpleConstantBuffer : register(b0)
    {
        matrix model;
        matrix view;
        matrix projection;
    };

    Which maps to this data structure in C++ 

    public value struct WorldModelProjection
    {
    	float4x4 model;
    	float4x4 view;
    	float4x4 projection;
    };
    
    template <class T>
    struct ConstantBuffer
    {
    	T Data;
    	Microsoft::WRL::ComPtr<ID3D11Buffer> Buffer;
    	void Initialize(ID3D11Device1* device, D3D11_BIND_FLAG bindflag = D3D11_BIND_CONSTANT_BUFFER)
    	{
    		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(T), bindflag);
    		HR2Ex(device->CreateBuffer(&constantBufferDesc, nullptr, &Buffer));
    	}
    	void UpdateBuffer(Microsoft::WRL::ComPtr<ID3D11DeviceContext1> ctxt)
    	{
    		ctxt->UpdateSubresource(Buffer.Get(), 0, NULL, &Data, 0, 0);
    	}
    };

    And it works fine.
    Now I am changing my HLSL and C++ structure as follows

    cbuffer SimpleConstantBuffer : register(b0)
    {
        matrix model;
        float3 modelcenter;
        matrix view;
        matrix projection;
    };
    
    
    public value struct WorldModelProjection
    {
    	float4x4 model;
    	float3 modelcenter;
    	float4x4 view;
    	float4x4 projection;
    };

    i.e. I added the "float3 modelcenter" property.

    When I call Initialize() on ny ConstandBuffer I got a failed HRESULT: E_INVALID arg...

    What is wrong with my HLSL data? I'm kind of new to them.. :~

    Edit: I found out the light problem, but it will still be nice to know why I can't modify the constant buffer that way!!!
    • Edited by Lloyd Tuesday, December 18, 2012 12:52 PM
    Tuesday, December 18, 2012 12:06 PM

Answers

  • Hi, lloyd

       I almost have the same question, I just want to use a float value, but creating constant buffer failed with E_INVALIDARG. At last, I use XMFLOAT4(the same as float4) and it's OK, actually I only use the XMFLOAT4.x to pass my parameter.  You can have a try.

    • Marked as answer by Lloyd Tuesday, January 1, 2013 1:27 PM
    Friday, December 28, 2012 1:45 AM

All replies

  • Try using the Direct3D Debug Device (D3D11_CREATE_DEVICE_FLAG_DEBUG) as it will likely give you some information about why it triggered E_INVALID.

    • Proposed as answer by Jesse Jiang Monday, December 24, 2012 3:12 AM
    Tuesday, December 18, 2012 10:17 PM
  • I'm pretty sure I have a debug flag on, must be this one (I'll check tonight when I get back home)

    I know I checked as I was hoping to have some diagnostic info appear in my output window (in visual studio)!

    But I think there was none! :'(

    Come to think of it I'm not entirely sure I did check the output window.. mm.... should double check tonight...

    Tuesday, December 18, 2012 11:54 PM
  • Hi,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 24, 2012 3:12 AM
  • Well, there is nothing in the output window!!! :'(

    And the debug flag is on, and I have set my project to mixed debugging (C# + C++) and it works (I can break and step in C++, OutputDebugString work, etc...)

    I won't be able to investigate that anymore until next year (flying out for Christmas in 2 hours...)

    I didn't insist because I was able to not use my additional parameter and I was playing with other thing that HLSL yet....

    But I definitely like to be able to debug HLSL early next year!!

    Monday, December 24, 2012 4:17 AM
  • Hi, lloyd

       I almost have the same question, I just want to use a float value, but creating constant buffer failed with E_INVALIDARG. At last, I use XMFLOAT4(the same as float4) and it's OK, actually I only use the XMFLOAT4.x to pass my parameter.  You can have a try.

    • Marked as answer by Lloyd Tuesday, January 1, 2013 1:27 PM
    Friday, December 28, 2012 1:45 AM
  • Just tried and it worked!

    Thanks for the tip!! :)

    Tuesday, January 1, 2013 1:27 PM
  • “If the bind flag is D3D11_BIND_CONSTANT_BUFFER, you must set the ByteWidth value in multiples of 16”
    See the MSDN ,notably the remarks.
    Monday, December 23, 2013 8:56 AM