Sign in to follow this  
Mathy

[SlimDX] Instancing? Shader?

Recommended Posts

Alright, so I want to do hardware instancing, but my render loop draws nothing. Here's my HLSL code for the instancing thing:
float4x4 world;
struct VS_INPUT {
	float4 Pos : POSITION;
};
struct VS_OUTPUT {
	float4 Pos : POSITION;
};
void vs(in VS_INPUT In, out VS_OUTPUT Out) {
	Out.Pos=mul(In.Pos,world);
};
technique m {
    pass P0 {
        VertexShader = compile vs_2_0 vs();        
    }
}
And now, here's my render procedure:
    Public Sub Render()

        If DEVICE Is Nothing OrElse RENDER_PAUSE Then Exit Sub

        DEVICE.Clear(ClearFlags.Target, BACKGROUND_COLOR, 0.0F, 0)
        DEVICE.BeginScene()

        SetupLights()

        DEVICE.Material = SELECTED_COMPUTER_MATERIAL

        Try

            Dim RandomEngine As New Random
            Dim CoordinateData(10 - 1) As Vector3
            For Index As Integer = 0 To 10 - 1
                CoordinateData(Index) = New Vector3(RandomEngine.NextDouble * VIEWPORT_SIZE.Width, RandomEngine.NextDouble * VIEWPORT_SIZE.Height, ZOOMING)
            Next

            Dim Buffer As New VertexBuffer(DEVICE, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero) * 10, Usage.WriteOnly, VertexFormat.Position, Pool.Default)
            Buffer.Lock(0, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero) * 10, LockFlags.None).WriteRange(CoordinateData)
            Buffer.Unlock()

            DEVICE.SetStreamSourceFrequency(0, 10, StreamSource.IndexedData)
            DEVICE.SetStreamSource(0, VIRUS_MESH.VertexBuffer, 0, VIRUS_MESH.BytesPerVertex)

            DEVICE.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData)
            DEVICE.SetStreamSource(1, Buffer, 0, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero))

            Dim Declaration As New VertexDeclaration(DEVICE, VertexDeclaration)

            DEVICE.VertexDeclaration = Declaration
            DEVICE.Indices = VIRUS_MESH.IndexBuffer

            OBJECT_EFFECT.SetValue(New EffectHandle("world"), DEVICE.GetTransform(TransformState.World))

            Dim Passes As Integer = OBJECT_EFFECT.Begin(FX.None)
            For Pass As Integer = 0 To Passes - 1
                OBJECT_EFFECT.BeginPass(Pass)
                DEVICE.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, VIRUS_MESH.VertexCount, 0, VIRUS_MESH.FaceCount)
                OBJECT_EFFECT.EndPass()
            Next
            OBJECT_EFFECT.End()

            DEVICE.SetStreamSourceFrequency(0, 1, 0)
            DEVICE.SetStreamSourceFrequency(1, 1, 0)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        DEVICE.EndScene()
        DEVICE.Present()

    End Sub
Any ideas to why this isn't working?

Share this post


Link to post
Share on other sites
Quote:
Original post by Mathy
Any ideas to why this isn't working?


Many reasons.

Going in order, down your post. There may be more.

---

Your shader input struct is not setup for instancing.

You don't seems to have a pixel shader, "return float4(0,0,0,0);" should suffice.

You've got lots of stuff that looks like FFP code, Materials, Lights etc for the FFP will not work with shaders, you need to write this all yourself.

Where do you define your VertexDeclaration? I imagine it's not setup for instancing.

Share this post


Link to post
Share on other sites
Here is my VertexDeclaration:

Public VertexDeclaration() As VertexElement = New VertexElement() {
New VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
VertexElement.VertexDeclarationEnd
}

Share this post


Link to post
Share on other sites
*sigh* I wish I'd never mentioned instancing in the other topic.

You need to understand what you're actually doing when you're drawing one object with a shader before you try to do instancing, and it's clear you don't.

Work on getting a single object drawn in the position of your choice with your shader first, then you can expand it to instancing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this