简体   繁体   中英

DX11 minimum buffer sizes

After creating a few constant buffers, some of them started to not initialize correctly.

A constant buffer is created with a struct called MiscStream, which is defined:

struct MiscStream {
    int rw,rh;
    float shine_factor;
    float dist_factor;
    int random_number;
};

And initialized with:

D3D11_BUFFER_DESC dx_misc_buffer_desc;
ID3D11Buffer* dx_misc_buffer;

ZeroMemory(&dx_misc_buffer_desc,sizeof dx_misc_buffer_desc);

    dx_misc_buffer_desc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
    dx_misc_buffer_desc.Usage=D3D11_USAGE_DYNAMIC;
    dx_misc_buffer_desc.ByteWidth=sizeof(MiscStream);
    dx_misc_buffer_desc.StructureByteStride=0;
    dx_misc_buffer_desc.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE;
    dx_misc_buffer_desc.MiscFlags=0;

hr=dx_device->CreateBuffer(&dx_misc_buffer_desc,NULL,&dx_misc_buffer);

hr would be returned as E_INVALIDARG, and the program would fail.

Many of the other constant buffers are set up this way, but they all had vectors and matrices in their structures.

I changed the MiscStruct to

struct MiscStream {
    D3DXVECTOR3 __r;
    int rw,rh;
    float shine_factor;
    float dist_factor;
    int random_number;
};

And it worked without error.

Is there a minimum structure size? What is happening?

From MSDN :

For a constant buffer ( BindFlags of D3D11_BUFFER_DESC set to D3D11_BIND_CONSTANT_BUFFER), you must set the ByteWidth value of D3D11_BUFFER_DESC in multiples of 16, and less than or equal to D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT.

So you were probably failing on the "multiple of 16" restriction. Depending on your packing settings, this can be different from adding up the sizes of the individual elements so check with sizeof() to see what the real struct size is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM