on d3d11 at the end of error message?

hi,

Trying to convert a library of sdf functions to HLSL, they’re in GLSL I believe and the warning at the top says excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays.

So I’m trying to convert this library to a cginc file for use in Unity. I’ve got a one error if I comment it out it works…the error is "syntax error: unexpected token ‘[’

I take it there’s a problem/differnce with initialising the array in hlsl/glsl

const float3 GDFVectors[19] = float3[]
(
    normalize(float3(1, 0, 0)),
    normalize(float3(0, 1, 0)),
    normalize(float3(0, 0, 1)),

    normalize(float3(1, 1, 1 )),
    normalize(float3(-1, 1, 1)),
    normalize(float3(1, -1, 1)),
    normalize(float3(1, 1, -1)),

    normalize(float3(0, 1, PHI+1)),
    normalize(float3(0, -1, PHI+1)),
    normalize(float3(PHI+1, 0, 1)),
    normalize(float3(-PHI-1, 0, 1)),
    normalize(float3(1, PHI+1, 0)),
    normalize(float3(-1, PHI+1, 0)),

    normalize(float3(0, PHI, 1)),
    normalize(float3(0, -PHI, 1)),
    normalize(float3(1, 0, PHI)),
    normalize(float3(-1, 0, PHI)),
    normalize(float3(PHI, 1, 0)),
    normalize(float3(-PHI, 1, 0))
);

I always have problems remembering the syntax for arrays in hlsl

float2 valueName[2] = {
float2(0,1),
float2(0,2)
};

1 Like

Ahhh it was the sqiggly brackets! Totally missed that last night, thank you!

I’m still having an issue after fixing it. Is there anything else here that could be returning a different value in hlsl to GLSL?

abs,dot,pow, max seem to the only functions used, and I know they’re fine usually. Could it be the normalizing of each element in the array that messing things up. There are also two fGDF functions, labelled exactly the same thing, I’m not sure you’re allowed to do that!

 float3 GDFVectors[19] =
{
    normalize(float3(1, 0, 0)),
    normalize(float3(0, 1, 0)),
    normalize(float3(0, 0, 1)),

    normalize(float3(1, 1, 1 )),
    normalize(float3(-1, 1, 1)),
    normalize(float3(1, -1, 1)),
    normalize(float3(1, 1, -1)),

    normalize(float3(0, 1, PHI+1)),
    normalize(float3(0, -1, PHI+1)),
    normalize(float3(PHI+1, 0, 1)),
    normalize(float3(-PHI-1, 0, 1)),
    normalize(float3(1, PHI+1, 0)),
    normalize(float3(-1, PHI+1, 0)),

    normalize(float3(0, PHI, 1)),
    normalize(float3(0, -PHI, 1)),
    normalize(float3(1, 0, PHI)),
    normalize(float3(-1, 0, PHI)),
    normalize(float3(PHI, 1, 0)),
    normalize(float3(-PHI, 1, 0))
};
//
// Version with variable exponent.
// This is slow and does not produce correct distances, but allows for bulging of objects.
float fGDF(float3 p, float r, float e, int begin, int end) {
    float d = 0;
    for (int i = begin; i <= end; ++i)
        d += pow(abs(dot(p, GDFVectors[i])), e);
    return pow(d, 1/e) - r;
}
// What is different in the title of these functions? How do I know which is getting called?

// Version with without exponent, creates objects with sharp edges and flat faces
float fGDF(float3 p, float r, int begin, int end) {
    float d = 0;
    for (int i = begin; i <= end; ++i)
        d = max(d, abs(dot(p, GDFVectors[i])));
    return d - r;
}

// Primitives follow:

float fOctahedron(float3 p, float r, float e) {
    return fGDF(p, r, e, 3, 6);
}

float fDodecahedron(float3 p, float r, float e) {
    return fGDF(p, r, e, 13, 18);
}

float fIcosahedron(float3 p, float r, float e) {
    return fGDF(p, r, e, 3, 12);
}

float fTruncatedOctahedron(float3 p, float r, float e) {
    return fGDF(p, r, e, 0, 6);
}

float fTruncatedIcosahedron(float3 p, float r, float e) {
    return fGDF(p, r, e, 3, 18);
}

float fOctahedron(float3 p, float r) {
    return fGDF(p, r, 3, 6);
}

float fDodecahedron(float3 p, float r) {
    return fGDF(p, r, 13, 18);
}

float fIcosahedron(float3 p, float r) {
    return fGDF(p, r, 3, 12);
}

float fTruncatedOctahedron(float3 p, float r) {
    return fGDF(p, r, 0, 6);
}

float fTruncatedIcosahedron(float3 p, float r) {
    return fGDF(p, r, 3, 18);
}

It shouldn’t. And the other common functions you’re using should have identical behaviors between HLSL and GLSL. Cursory look over what you have I don’t see anything that should be wrong. Sorry.