geometry shader: limit to number of instances?

I wrote a simple geometry shader that draws multiple instances of a model (in this case Ralph…), and I am getting this error when outputting more than twenty Ralph:

gp4gp: error: too many total output components.

Is it related to the Cg profile (gp4gp), or am I doing something wrong?

I’m guessing the number of vertices that the geometry shader outputs is more than the driver accepts.

Can you post your geometry shader?

I am actually surprised to have this problem outputting only twenty Ralph. How many trees did you have in the demo on the blog?

This is my geometry shader, as you can see I am not doing anything else that emitting the displaced vertices.

TRIANGLE void gshader(AttribArray<float4> position : POSITION,  
		            AttribArray<float4> texcoords : TEXCOORD0,
		            AttribArray<float4> normals : TEXCOORD1,
                      uniform float4x4 mat_modelproj)
  int n = 10;
  for(int r=n; r<n r+=1) {
    for(int i=0; i<position.length; i++) {
      float4 offset = mul(mat_modelproj,float4(r,0,0,0));
      float4 npos = position[i] + offset;

Hm… have you considered using true instancing (with setInstanceCount), rather than instancing using a geometry shader?

I didn’t use a geometry shader for the trees in the blog post. I had somewhere over 100.000 trees, and I used setInstanceCount for instancing.

btw I’m seeing a setInstanceCount in the ShaderAttrib class as well - could really somebody put up a quick working snip to enjoy us? this is quite an interesting topic

What is setInstanceCount? Is it a panda3d function?

Yes, it’s a function of NodePath. It is the interface to hardware geometry instancing.
Have you seen this blog post?

sorry again for the intermission but I’m reading this:

getting almost nothing. I’m still guessing 2 lines or code could help many ppl.

I am not following at all… Mostly because I can’t find any decent online documentation about cg geometry shader and true instancing (I am not referring to panda3D docs).

So my guess at the moment is: are you calling n times for each vertex the shaders, where n is the number of instances? And if my guess is correct, how do you render one vertex multiple times drawing it once? As far as I know the geometry shader is the only one supposed to do this.

INSTANCEID is a custom semantic?

INSTANCEID is a Cg semantic. It should have been covered in the documentation, but it isn’t.

As my blog post states, you call setInstanceCount(n) once. This means that the vertex shader is executed n times, every time passing a different value for the varying integer assigned to INSTANCEID. Based on that, you can do different behavior based on different instances.

Also see:

Thanks! So how do you call the vertex shader multiple times? At Cg level I mean?

That’s handled by the graphics card. All you need to do is call setInstanceCount.

No, I know, just for my personal interest I was wondering what’s the function of the Cg Runtime to be called?

mmh, I am doing this:


uniform int l_idx:INSTANCEID
vpos.x += l_idx; 
l_position = mul(mat_modelproj, float4(vpos,1));

Shouldn’t I see ten instances offsetted by 1?

ops… settled l_idx to be uniform… now it works fine.

How can I get Instance Id in glsl vertex shader?

Use the gl_InstanceID or gl_InstanceIDARB (I’m not sure which) variable. This is not really a Panda3D-related question, though - I’m sure there are lots of resources on the Internet about this.

Thanks, rdb :smiley:
I did search the web, but seems that my keywords weren’t quite correct.

You can find more infos in the ARB_draw_instanced specification at: … tanced.txt