1

I am currently working on a shader where the amount of vertices are not defined by the amount of triangles, my shader puts a cuber every X units.

This means that I cannot know in advance how many cubes it will create and I do not know many vertices it will take.

However you have to put a maxvertexcount[x]. What do I put in the x?

How do I work around this?

Note: I am a beginner at geometry shaders and know next to nothing about them.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
Subject009
  • 79
  • 1
  • 10

1 Answers1

1

You do know how many vertices it will take ("take" as in input) per-invocation.

That is defined by the primitive topology; a triangle (strip), for instance takes 3 and triangle adjacency takes 6. MaxVertexCount absolutely has to be known, because there is an implementation limit to the number of vertices you can output per invocation.

If you hit that limit on a single invocation, then you will have to force the GPU to invoke your Geometry Shader multiple times (Shader Model 5.0 feature). D3D refers to this process as "Geometry Shader Instancing" and gives uint InstanceID : SV_GSInstanceID to identify which duplicate invocation (instance) is executing.

Your description of the problem is actually making this difficult to answer definitively, could you include some HLSL code to show what you are actually trying to do?

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • I am trying to convert a building into bricks to I can gradually build it up. so 1 triangle -> multiple (amount depending on triangle size) bricks. Isn't Maxvertexcount the number you're outputting? If it's the input then it's just 3 (using trianglelist). – Subject009 Jun 07 '14 at 16:42
  • Yes, but you were speaking of vertices ***taken*** in part of your question. To me, that implies *input*. Sort of a *consumed* versus *produced* thing. Maxvertexcount is the maximum number of vertices a single invocation ***may*** output, you are free to output as few as 3 vertices if you want if you are outputting triangles. But there is an upper-bound to the number of vertices you can output in a single invocation as I mentioned. It is usually on the order of 256 or so; that means a single invocation of your GS can output up to 85 triangles if you use MaxVertexCount = 256. – Andon M. Coleman Jun 07 '14 at 17:11
  • The reason you might not simply set the maximum vertex count to 256 and forget about it is because D3D uses that information to improve the parallel execution efficiency. GS outputs have to preserve the order of execution for drawing to work properly, so that means each GS has to reserve n-many vertices in a post-transform cache buffer. If you set that value too high, you hurt the efficiency of your GS cache. To be honest it is not a huge issue on modern DX11 hardware, they have large caches but when geometry shaders first came out (DX10) it was a major issue. – Andon M. Coleman Jun 07 '14 at 17:14
  • I see but then my problem still remains, if it's a really large triangle i might have to output 2056 vertices or even more and if i set a number as high as that it gives an error. – Subject009 Jun 07 '14 at 17:57
  • Correct, you cannot output more than 256 vertices in a single invocation. You need GS instancing to be able to do that. D3D11 guarantees up to 32 instances, so that would allow you to output 8,192 vertices or 2,730 triangles by running your GS 32 times. – Andon M. Coleman Jun 07 '14 at 17:59
  • Ok but I'm a beginner so... How do I run my GS multiple times? – Subject009 Jun 07 '14 at 18:05
  • @Subject009 : subdividing is a job which is more designed for tesselation shader, geometry shader is not so efficient for this type of task. – mrvux Jun 08 '14 at 14:21