0
\$\begingroup\$

I am writing a shader to achieve a laser show effect example. For now I am just focusing on the lines. One line ist represented as one triangle. When I save my shadercode, unity gets inactive and I can see in the task manager that the shader compiler has 30% cpu -> is compiling. But this takes forever, and when I restart unity, it isn't able to load the shader from the assets. I found out that this doesn't happen when I comment the line:

const int TRI_STRIP[TAM] = tmpTriStrip;

Obviously I then get an error that TRI_STRIP is undefined, but the shadercompiler did its work and Unity is still running. Then I put the line back into the code and the above desribed behaviour appears. Thanks for any tipps and advices.

Here is the complete geometryshader code:

    void myGeometryShader(point gIn vert[1], inout TriangleStream<v2f> triStream)
{
    float f = _PointSize / 20.0f * sin(_Time * _Speed); //half size

    float4 vctmp[TAM];

    int i;
    float y;
    for (i = 0; i < TAM; i += 3) {

        y = sin(_Time * _Speed + _SinValue) * _MovHeight + _HeightOffset;
        vctmp[i] = float4(0.0f, 0.0f, 0.0f, 0.0f);
        vctmp[i + 1] = float4(_Distance, y, i / _MarginFactor - _EndWidth / 2.0f, 0.0f);
        vctmp[i + 2] = float4(_Distance, y, i / _MarginFactor + _EndWidth / 2.0f, 0.0f);
    }

    const float4 vc[TAM] = vctmp;


    float2 UV1tmp[TAM];
    for (i = 0; i < TAM; i += 3) {
        UV1tmp[i] = float2(0.0f, 0.0f);
        UV1tmp[i + 1] = float2(1.0f, 0.0f);
        UV1tmp[i + 2] = float2(1.0f, 1.0f);
    }

    const float2 UV1[TAM] = UV1tmp;

    int tmpTriStrip[TAM];

    for (i = 0; i < TAM; i++) {
        tmpTriStrip[i] = 1;
    }

    const int TRI_STRIP[TAM] = tmpTriStrip;

    v2f v[TAM];


    // Assign new vertices positions 
    for (i = 0; i < TAM; i++) { v[i].pos = vert[0].pos + vc[i]; v[i].col = vert[0].col; }

    // Assign UV values
    for (i = 0; i < TAM; i++) v[i].uv_MainTex = TRANSFORM_TEX(UV1[i],_MainTex);

    // Position in view space
    for (i = 0; i < TAM; i++) { v[i].pos = mul(UNITY_MATRIX_MVP, v[i].pos); }

    // Build the cube tile by submitting triangle strip vertices
    for (i = 0; i < TAM / 3; i++)
    {
        triStream.Append(v[TRI_STRIP[i * 3 + 0]]);
        triStream.Append(v[TRI_STRIP[i * 3 + 1]]);
        triStream.Append(v[TRI_STRIP[i * 3 + 2]]);

        triStream.RestartStrip();
    }
}
\$\endgroup\$
1
\$\begingroup\$

The looks like a compiler bug.

But in any case:

  • You don't need the copy into a const array, just use the non-const one.
  • You don't need those large arrays, from what I see everything in the shader can be done in a single for (i = 0; i < TAM; i += 3) loop with small arrays of 3.

That should work around the compiler bug and probably make the shader faster.

\$\endgroup\$
  • \$\begingroup\$ Thanks. For verts and uvs I can use normal arrays, but for the triangleStrip it has to be constant. Why are smaller arrays sufficient? \$\endgroup\$ – Freshchris Jan 16 '16 at 19:19
  • \$\begingroup\$ The array is just filled with 1s: ` int tmpTriStrip[TAM]; for (i = 0; i < TAM; i++) { tmpTriStrip[i] = 1; } const int TRI_STRIP[TAM] = tmpTriStrip;` \$\endgroup\$ – Stephane Hockenhull Jan 16 '16 at 21:13
  • 1
    \$\begingroup\$ And you can calculate the values 3 at a time in 1 loop instead of calculating an entire array and then using it in a different loop. \$\endgroup\$ – Stephane Hockenhull Jan 16 '16 at 21:15
  • \$\begingroup\$ Ahh ok now i see, thanks. I will definitly do that. \$\endgroup\$ – Freshchris Jan 16 '16 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.