HLSL 数组索引返回意外值

发布于 2025-01-09 04:48:49 字数 936 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2025-01-16 04:48:49

我弄清楚了为什么数组没有输出正确的值。
在 HLSL 中,在同一行中声明和初始化数组时,必须包含 static 关键字。

我的 HLSL 代码应该是:(

#pragma kernel ApplyMarchingCubes

static 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]);
}

注意第三行的 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:

#pragma kernel ApplyMarchingCubes

static 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]);
}

(Notice static on the third line, it was not there before).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文