简体   繁体   English

常量缓冲区中的2D数组

[英]2D array in constant buffer

Im trying to pass a 2D float array to a constant buffer: 我试图将2D浮点数组传递给常量缓冲区:

//In the shader:
    cbuffer myBuffer
    {
        other buffer elements
        .
        .
        float myArray[16][16];
    };

//In the CPU:
    struct  myBuffer_struct
    {
        other buffer elements
        .
        .
        float myArray[16][16];
    };

But im having a lot of problems dealing with the padding. 但是即时通讯在填充方面有很多问题。 I tried using 我尝试使用

float4[size/4][size] float4 [size / 4] [size]

in my cbuffer and a lot of other type combinations but I cant access to my array by indexation in any way. 在我的cbuffer和许多其他类型组合中,但是我无法以任何方式通过索引访问我的数组。 What is the proper way to do this? 正确的方法是什么?

Thank you. 谢谢。

I've had this issue and it comes down to basically the alignment of the buffer. 我遇到了这个问题,基本上可以归结为缓冲区的对齐。 Your HLSL cbuffer definition most definitely will be padding differently to what you have defined in your struct. 您的HLSL cbuffer定义绝对会与您在结构中定义的填充不同。

The alignment probably along 16 byte (4 floats) alignment. 对齐方式可能沿16个字节(4个浮点数)对齐。 In my code, I was writing 4 floats out into a buffer. 在我的代码中,我正在写4个float到一个缓冲区中。 Like this below, as the array alignment was different in the cbuffer. 像下面这样,因为cbuffer中的数组对齐方式不同。

         for (int i = 0; i < 8; i++)
        {
            stream.Write<float>(m_waveLengths[i] ); 
            stream.Write<float>(m_waveSpeeds[i] );
            stream.Write<float>(m_amplitudes[i] ); 
            stream.Write<float>(m_steepness[i]); 
        }

To read this, I used a float4 array definition. 要阅读此内容,我使用了float4数组定义。

// hlsl definition
float4 Wave[8];  

I then referenced the relevant item as Wave[0].x, Wave[0].y, Wave[0].z, Wave[0].w 然后,我将相关项目引用为Wave [0] .x,Wave [0] .y,Wave [0] .z,Wave [0] .w

The memory alignment would make the buffer 4 times bigger if I didn't pack it like this. 如果不这样打包,内存对齐将使缓冲区大4倍。 This is because in the HLSL code, the buffer definition seems to of aligned each element of the array along 16 byte boundries (4 x floats). 这是因为在HLSL代码中,缓冲区定义似乎沿16个字节边界(4个浮点数)对齐了数组的每个元素。 So instead, I interweaved my 4 arrays into 1 array and used the properties of float4 to reference it. 因此,我将4个数组交织为1个数组,并使用float4的属性对其进行引用。

because the alignment of float waveLengths[8] would of meant that I would have to write it into the buffer like this: 因为float waveLengths [8]的对齐将意味着我必须像这样将其写入缓冲区:

   for (int i = 0; i < 8; i++)
        {
            stream.Write<float>(m_waveLengths[i] ); 
            stream.Write<float>(0.0f);
            stream.Write<float>(0.0f); 
            stream.Write<float>(0.0f); 
        }

For some reason (and I am probably not setting a certain HLSL compiler directive), using arrays in the Cbuffer had some quirks where it would pad each element to a 16 byte boundary. 由于某种原因(我可能未设置某个HLSL编译器指令),在Cbuffer中使用数组会有一些奇怪之处,那就是它将每个元素填充到16字节边界。

So, for your float myArray[16][16], I would assume that you look at the alignment, you may have to write the buffer for this out in a similar manner, padding out 12 bytes after each element in the array. 因此,对于您的float myArray [16] [16],我假设您看一下对齐方式,可能必须以类似的方式为此写缓冲区,在数组中的每个元素之后填充12个字节。 I'm sure someone will respond with correct compiler directive to get rid of this quirk, I just solved this a while ago and your problem looks similar to what I had. 我确定有人会用正确的编译器指令来响应,以摆脱这种怪癖,我刚刚解决了这一问题,而您的问题看起来与我所遇到的类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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