Hi,
i could pull each of my hair because i can' t find my bug for hours i really got headache
in my Indie game engine i use DrawIndexed
and want to use the VertexID in my Vertex Shader in Directx 11 ( working with win7).
here the manual of Microsoft
Using System-Generated Values
VertexID
A vertex id is used by each shader stage to identify each vertex. It is a 32-bit unsigned integer whose default value is 0. It is assigned to a vertex when the primitive is processed by the IA stage. Attach the vertex-id semantic to the shader input declaration to inform the IA stage to generate a per-vertex id.
The IA will add a vertex id to each vertex for use by shader stages. For each draw call, the vertex id is incremented by 1. Across indexed draw calls, the count resets back to the start value. For ID3D11DeviceContext::DrawIndexed and ID3D11DeviceContext::DrawIndexedInstanced, the vertex id represents the index value. If the vertex id overflows (exceeds 2³²– 1), it wraps to 0.
For all primitive types, vertices have a vertex id associated with them (regardless of adjacency).
end of Microsoft Text.
When setting my Material_idx manually e.g. to 25 or another value the shape is displayed accordingly
But when i try to get the actual shape idx from the system generated vertexID i always get the attributes of shape 0
But the manual states when call drawindexed the vertex id represents the index value.
The Question is:
Does this mean, that the value of the vertex id will alway stay in the range of e.g. ( 0 to 7 ) for a cube Vertexbuffer even i do a draw call with a binded indexbuffer of e.g. 36 000 indexes all referring to the same 8 vertexies in the tiny small vertexbuffer ? ( Trying to draw 1000 different attributed shapes )
In the Blog Post http://www.joshbarczak.com/blog/?p=667 "Why Geometry Shaders Are Slow" there is chapter where the author does exactly the same i want to do.
Big Indexbuffer and tiny vertexbuffer to draw multiple cubes but NOT with the drawinstanced method.
Method 2: The “Instancing Sucks” Way
Let’s do the same thing, except let’s not use instancing. The reasons for this will become clear shortly. Now, we don’t want to go doing one drawcall per box, because that would just introduce more overhead. We also don’t want to duplicate the same unit cube 250K times. That would consume unnecessary bandwidth. Instead, we’ll do this by generating a gigantic index buffer and doing SV_VertexID math. We know that cube i will reference vertices 8*i through 8*i+7, so we can figure out our own local instance and vertex ID from a flat index buffer. The only drawback is that now we need to fetch our vertex and instance data explicitly:
uniform row_major float4x4 g_ViewProj;
Buffer<float4> Verts;
Buffer<float4> XForms;
float4 main(
uint vid : SV_VertexID ) : SV_Position
{
uint xform = vid/8;
float4 v = Verts[vid%8];
float4 R0 = XForms[3*xform];
float4 R1 = XForms[3*xform+1];
float4 R2 = XForms[3*xform+2];
// deform unit box into desired oriented box
float3 vPosWS = float3( dot(v,R0), dot(v,R1), dot(v,R2) );
// clip-space transform
return mul( float4(vPosWS,1), g_ViewProj );
}
DrawBoneShapes_VSOut DrawShapeColored_unique_sequenz_VertexShader(DrawShapeColored_VSIn input, uint vertexID : SV_VertexID)
{
uint a_Material_idx = vertexID;
// Test Case Cube:
// has 8 Vertecies
// has 6 Sides * 2 Triangles * 3 Vertecies = 36 indexes
// vertexID never gets greater than 8 (I assume) so allways the attributes of 0 th Cube are displayed
a_Material_idx = vertexID/36;
// set manually attributes of 26 th Cube ( all works fine !! )
a_Material_idx = 25;
DrawBoneShapes_VSOut Output;
float4x4 WorldViewProj_Shape = mul(BoneShapeList_WVP[a_Material_idx], WorldViewProj);
Output.Position = mul(input.Position, WorldViewProj_Shape);
Output.Normal = float3(0, 0, 0);
Output.TexCoord = float2(0, 0);
Output.Depth = 1;
Output.Material_idx = a_Material_idx;
return Output;
}