尝试访问 StructuredBuffer 时编译错误
Compile error when trying to access StructuredBuffer
我想在计算着色器中访问 StructuredBuffer<int>
,但出现错误:
Shader error in 'Particle.compute': array, matrix, vector, or indexable object type expected in index expression at Particle.compute(28) (on 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
s 的当前计数,if
子句防止消耗比可用更多的粒子。
这是一个老问题,所以我假设你已经解决了它,但无论如何这里是答案:
当 particleCount 已经是缓冲区时,您将其声明为 int。
要么将名称更改为 int currentParticleCount = particleCount[0];
,要么不使用临时变量:
if (flatID >= particleCount[0]) return;
我想在计算着色器中访问 StructuredBuffer<int>
,但出现错误:
Shader error in 'Particle.compute': array, matrix, vector, or indexable object type expected in index expression at Particle.compute(28) (on 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
s 的当前计数,if
子句防止消耗比可用更多的粒子。
这是一个老问题,所以我假设你已经解决了它,但无论如何这里是答案:
当 particleCount 已经是缓冲区时,您将其声明为 int。
要么将名称更改为 int currentParticleCount = particleCount[0];
,要么不使用临时变量:
if (flatID >= particleCount[0]) return;