directxhlsldirectcompute

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


In a DirectCompute shader, having a function taking an string type argument, how to access individual characters ?

Example:

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

The compiler complain about S[I]: "error X3121: array, matrix, vector, or indexable object type expected in index expression".

Any idea?


Solution

  • From MS docs:

    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.

    Strings exist in HLSL, but there’s very little you can do with them. Depending on your needs, you might want to pass the string to the shader as an array of instead of a string, or as a RWStructuredBuffer of bytes, then perform the conversion to/from ASCII.