DirectX 计算着色器 (HLSL):如何访问字符串中的单个字符?

DirectX compute shader (HLSL): how to access individual characters in a string?

在 DirectCompute 着色器中,有一个接受字符串类型参数的函数,如何访问单个字符?

示例:

uint TestFunc(string S, uint I)
{
    return uint(S[I]);
}

编译器抱怨 S[I]:"error X3121: array, matrix, vector, or indexable object type expected in index expression"。

有什么想法吗?

来自 MS 文档:

HLSL also supports a string type, which is an ASCII string. There are no operations or states that accept strings, but effects can query string parameters and annotations.

字符串存在于 HLSL 中,但您几乎无能为力。根据您的需要,您可能希望将字符串作为数组而不是字符串传递给着色器,或者作为 RWStructuredBuffer 字节,然后执行转换 to/from ASCII。