尝试访问StructuredBuffer时编译错误

时间:2017-06-05 17:59:13

标签: compute-shader directcompute

我想在计算着色器中访问StructuredBuffer<int>但我收到错误:

  

Particle.compute&#39;中的着色器错误:Particle.compute(28)(在d3d11上)索引表达式中预期的数组,矩阵,向量或可索引对象类型

代码:

#pragma kernel CSMain
#include "Assets/Uplus/ZCommon/Resources/ImageProcessing/UplusDirectCompute.cginc"

struct Particle
{
    float3 Position;
    float Mass;
};

Texture2D<float2> _terTx;

ConsumeStructuredBuffer<Particle> currentBuffer;
AppendStructuredBuffer<Particle> nextBuffer;
StructuredBuffer<int> particleCount;

float3 _terPos;
float _terSize, _terPhysicalScale, _resolution;

SamplerState _LinearClamp;
SamplerState _LinearRepeat;

#define _gpSize 512

[numthreads(_gpSize, 1, 1)]
void CSMain(uint3 dispatchID : SV_DispatchThreadID)
{
    int flatID = dispatchID.x;
    int particleCount = particleCount[0];

    if (flatID >= particleCount) return;

    Particle particle = currentBuffer.Consume();

    //Commented the rest of code

    nextBuffer.Append(particle);
}

错误指向行int particleCount = particleCount[0];。那是为什么?

着色器背后的整个想法是我们有两个缓冲区。我们从CPU中填充一些数据(我们称它们中的每一个Particle),然后在着色器中使用缓冲区中的数据,处理它然后附加到另一个缓冲区。然后我们交换缓冲区并进行另一次迭代。 particleCount缓冲区保存缓冲区保存的当前计数Particle,并且if子句可以防止消耗更多可用的粒子。

1 个答案:

答案 0 :(得分:0)

这是一个老问题,所以我假设你已经解决了它,但无论如何这里是答案:

当它已经是缓冲区时,您将粒子计数声明为 int。

要么将名称更改为 int currentParticleCount = particleCount[0];,要么只是不使用临时变量:

if (flatID >= particleCount[0]) return;
相关问题