HLSL 数组索引返回意外值
我正在 Unity 的 GPU 上执行一些 HLSL 代码,但在从数组中获取值时遇到问题。这是我的简化代码示例。
C#
ComputeBuffer meshVerticesBuffer = new ComputeBuffer(
15 * 1,
sizeof(float) * 3
);
marchingCubesShader.SetBuffer(0, "MeshVertices", meshVerticesBuffer);
marchingCubesShader.Dispatch(0, 1, 1, 1);
Vector3[] meshVertices = new Vector3[15 * 1];
meshVerticesBuffer.GetData(meshVertices);
meshVerticesBuffer.Release();
HLSL
#pragma kernel ApplyMarchingCubes
int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
我通过调试器在 C# 端观察 meshVertices
,第一项始终是 Vector3(0, 0, 0)
。我期待 Vector3(0, 0, -1)
的结果。我做错了什么?
I am executing some HLSL code on the GPU in Unity, but I am having issues with getting values out of an array. Here is my simplified code example.
C#
ComputeBuffer meshVerticesBuffer = new ComputeBuffer(
15 * 1,
sizeof(float) * 3
);
marchingCubesShader.SetBuffer(0, "MeshVertices", meshVerticesBuffer);
marchingCubesShader.Dispatch(0, 1, 1, 1);
Vector3[] meshVertices = new Vector3[15 * 1];
meshVerticesBuffer.GetData(meshVertices);
meshVerticesBuffer.Release();
HLSL
#pragma kernel ApplyMarchingCubes
int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
I am watching meshVertices
on the C# side through the debugger, and the first item is always a Vector3(0, 0, 0)
. I am expecting a result of Vector3(0, 0, -1)
. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚了为什么数组没有输出正确的值。
在 HLSL 中,在同一行中声明和初始化数组时,必须包含
static
关键字。我的 HLSL 代码应该是:(
注意第三行的
static
,它以前不存在)。I figured out why the array was not putting out the right values.
In HLSL, when declaring and initializing an array in the same line, you must include the
static
keyword.My HLSL code should have been:
(Notice
static
on the third line, it was not there before).