Skip to main content
GameDev.net gamedev.net
Using GameDev.net for your class this semester?
Learn more →
🔒 Locked

StartInstanceLocation, and SV_InstanceID

Started by ReaperSMS Nov 5, 2014 at 12:58 AM 1 replies 13.5k views
Original Post
ReaperSMS
ReaperSMS

So, I'm trying to use SV_InstanceID as an extra input to a shader, to pick from a small set of vertex colors in code.

It seems to completely ignore the last argument of DrawIndexedInstanced(), and start at 0 per draw call. This seems less than useful, as it would make it impossible to transparently split up an instanced draw call, and defeat a lot of the purpose of having the system value at all.

How would one be expected to use SV_InstanceID properly in this case? The vertex shader looks about like so:


struct VertexInput
{
    float4 position : POSITION;
    uint instanceid : SV_InstanceID;
};

struct VertexOutput
{
    float4 projPos : SV_Position;
    float4 color : COLOR0;
};

VertexOutput vs_main( const VertexInput input )
{
    VertexOutput output = (VertexOutput)0;

    output.projPos = mul( float4( input.position.xyz, 1.0f ), g_ViewProjection );

    if ( input.instanceid == 0 )
    {
        output.color = float4(1,0,0,1);
    }
    else if ( input.instanceid == 1 )
    {
        output.color = float4(0,1,0,1);
    }
    else
    {
        output.color = float4(0.5,0.5,0.5,1);
    }
    return output;
}

This results in it always picking red. If I instead dig a color out of a separate vertex buffer, via D3D11_INPUT_PER_INSTANCE_DATA, it works as expected.

How do I make d3d useful?

Buckeye
Buckeye




If I instead dig a color out of a separate vertex buffer, via D3D11_INPUT_PER_INSTANCE_DATA, it works as expected.
How do I make d3d useful?

You can make it useful by applying it as documented, which you just did by adding per-instance data. From the docs:

Instancing requires multiple vertex buffers: at least one for per-vertex data and a second buffer for per-instance data.


The StartInstanceLocation argument provides the start location of the per-instance data. Again, from the docs:


StartInstanceLocation [in]

Type: UINT

A value added to each index before reading per-instance data from a vertex buffer.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
ReaperSMS
ReaperSMS

Bleh, I see gl does the same thing. I suppose I shall have to put up with being terribly disappointed in the PC API's again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.