I'm using Unitys DrawProceduralIndirect function to directly render the content of a ComputeShader.

I setup the argumentBuffer for this function as follows:

    int[] csArgs = new int[] { ptsOnLine, 2, 0, 0};
    argsBuffer = new ComputeBuffer(4, 4, ComputeBufferType.IndirectArguments);

So every instance consists of ptsOnLine (lets say 10) vertices.

My vertexShader then looks like this:

fsInput vert(uint id : SV_VertexID, uint instanceId : SV_InstanceId)
    fsInput fsIn;
    float3 worldPos = linePoints[id + (instanceId * 10)];
    fsIn.pos = mul(UNITY_MATRIX_VP, float4(worldPos, 1.0f));
    return fsIn;

The question is simple. My problem is: (instanceId * 10). Is there a built in variable telling me the length of one instance? I couldn't find anything. I mean, its in the argument buffer so it shouldn't be too much of a problem? I could forward this value from the compute buffer but thats ugly. :)

Help is much appreciated!


The "built in" variable is contained in the args buffer.

From the unity docs "Buffer with arguments, bufferWithArgs, has to have four integer numbers at given argsOffset offset: vertex count per instance, instance count, start vertex location, start instance location."

So make sure you pass that argsBuffer to your shader and then use the first index.

  • \$\begingroup\$ Feels a bit redundant to pass it explicitly to the shader when it is already passed to DrawProceduralIndirect. Nevertheless, it seems that there is no built in variable, so this is the way to go. Thanks a lot! \$\endgroup\$
    – Christoph
    Aug 17 '17 at 14:27

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.