HLSL float bulk packing in a permanent buffer?

people.

I have a problem passing a floating point array to a vertex shader (HLSL) via a persistent buffer . I know that each "float" in the array below gets a 16 byte slot separately (space is equivalent to float4) due to the HLSL packing rule:

// C++ struct
struct ForegroundConstants
{
    DirectX::XMMATRIX transform;
    float bounceCpp[64];
};


// Vertex shader constant buffer
cbuffer ForegroundConstantBuffer : register(b0)
{
    matrix transform;
    float bounceHlsl[64];
};

      

(Unfortunately, the simple solution doesn't work here , after I made this change, nothing works)

While the C ++ data is transferred due to the packing rule, it is expanded in such a way that each "float" in the C ++ bounceCpp array falls into 16-byte space on its own in bounceHlsl . This resulted in a warning similar to the following:

ID3D11DeviceContext :: DrawIndexed: The size of the constant buffer in slot 0 of the vertex shader block is too small (320 bytes, 1088 bytes at least expected). This is fine, since out-of-bounds reads are defined to return 0. Perhaps the developer knows the missing data will not be used anyway. This is only a problem if the developer actually intended to bind a large enough persistent buffer for the expected shader.

The recommendation, as stated here and here , is to rewrite the HLSL constant buffer as follows:

cbuffer ForegroundConstantBuffer : register(b0)
{
    matrix transform;
    float4 bounceHlsl[16]; // equivalent to 64 floats.
};

static float temp[64] = (float[64]) bounceHlsl;

main(pos : POSITION) : SV_POSITION
{
    int index = someValueRangeFrom0to63;
    float y = temp[index];

    // Bla bla bla...
}

      

But that didn't work (i.e. ID3D11Device1 :: CreateVertexShader never returns). I am compiling things against Shader Model 4 Level 9_1 , can you spot anything I did wrong here?

Thanks in advance!:)

Regards Ben

+3


source to share


1 answer


One solution, although not optimal, is to simply declare your floating point array as

float4 bounceHlsl[16];

      

then process the index like



float x = ((float[4])(bounceHlsl[i/4]))[i%4];

      

where i is the index you need.

+1


source







All Articles