Jump to content

  • Log In with Google      Sign In   
  • Create Account


DirectX 9 Instancing (with SharpDX)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Opt7ons   Members   -  Reputation: 817

Like
0Likes
Like

Posted 11 July 2012 - 08:48 AM

I am having some trouble getting instancing to work with DX9 and Sharpdx.

My test triangle appears only in the first frame and then it disappears or fails to render for every loop after the first.

Here is what I have currently:


	 [StructLayout(LayoutKind.Sequential)]
	public struct VertexPositionColor
	{
		public Vector4 Position;
		public int Color;

		public VertexPositionColor(Vector4 position, int color)
		{
			this.Position = position;
			this.Color = color;
		}
	}

	public class Program
	{
		public static void Main(String[] args)
		{
			RenderForm form = new RenderForm("SharpDXTest Window");
			Direct3D d3d = new Direct3D();
			Device device = new Device(d3d, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing,
				new PresentParameters(form.ClientSize.Width, form.ClientSize.Height));

			device.SetRenderState(RenderState.Lighting, false);
			device.SetRenderState<Cull>(RenderState.CullMode, Cull.None);

			var effect = Effect.FromFile(device, "MiniCube.fx", ShaderFlags.None);
			var technique = effect.GetTechnique(0);
			var pass = effect.GetPass(technique, 0);

			// Prepare the vertex declaration
			var vertexElems = new[] {
		 new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
		 new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
				new VertexElement(1, 0,  DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate,  1),
				new VertexElement(1, 16, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate,  2),
				new VertexElement(1, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate,  3),
				new VertexElement(1, 48, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate,  4),
VertexElement.VertexDeclarationEnd
		 };
			var vertexDecl = new VertexDeclaration(device, vertexElems);

			// Prepare matrices
			var view = Matrix.LookAtRH(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY);
			var proj = Matrix.OrthoRH(form.ClientSize.Width, form.ClientSize.Height, 0.1f, 1000000000.0f);
			device.SetRenderState(RenderState.Lighting, false);
			device.SetRenderState<Cull>(RenderState.CullMode, Cull.None);
			effect.SetValue("proj", proj);
			effect.SetValue("view", view);

			VertexBuffer vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
			vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
				new VertexPositionColor() {Position = new Vector4(0, 0, 1.0f, 1), Color = Color.Red.ToArgb()},
				new VertexPositionColor() {Position = new Vector4(110, 110, 1.0f, 1), Color = Color.Red.ToArgb()},
				new VertexPositionColor() {Position = new Vector4(110, 0, 1.0f, 1), Color = Color.Red.ToArgb()},
			});
			vertices.Unlock();

			Matrix mat = Matrix.Identity;
			//mat.Transpose();
			VertexBuffer instances = new VertexBuffer(device, 1 * 64, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
			instances.Lock(0, 0, LockFlags.None).WriteRange(new[] {
				mat
			});
			instances.Unlock();

			IndexBuffer indices = new IndexBuffer(device, 6, Usage.WriteOnly, Pool.Managed, true);
			indices.Lock(0, 0, LockFlags.None).WriteRange(new short[] {
				(short)0, (short)1, (short)2,
			});
			indices.Unlock();

			var world = Matrix.Translation(100, 100, 0);

			effect.Technique = technique;
			effect.SetValue("proj", proj);
			effect.SetValue("view", view);
			effect.SetValue("world", world);

			RenderLoop.Run(form, () =>
			{
				device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, (Color4)Color.CornflowerBlue, 1.0f, 0);

				device.BeginScene();

				effect.Begin(0);
				effect.BeginPass(0);

				effect.SetValue("proj", proj);
				effect.SetValue("view", view);
				effect.SetValue("world", world);
				device.SetStreamSourceFrequency(0, 3, StreamSource.IndexedData);
				device.SetStreamSource(0, vertices, 0, 20);
				device.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData);
				device.SetStreamSource(1, instances, 0, 64);
				
				device.VertexDeclaration = vertexDecl;
				device.Indices = indices;

				device.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);

				device.ResetStreamSourceFrequency(0);
				device.ResetStreamSourceFrequency(1);

				effect.EndPass();

				effect.End();

				device.EndScene();
				device.Present();
			});
		}


and the shader:

float4x4 world;
float4x4 proj;
float4x4 view;

sampler2D mySampler
{
	MinFilter = Point;
	MagFilter = Point;
};

struct VS_IN
{
float4 pos : POSITION0;
	float4 tex : COLOR0;
	float4 W0 : TEXCOORD0;
	float4 W1 : TEXCOORD1;
	float4 W2 : TEXCOORD2;
	float4 W3 : TEXCOORD3;
};

struct PS_IN
{
	float4 tex : COLOR0;
float4 pos : POSITION0;
};

PS_IN VS( VS_IN input )
{
	//float4x4 wor = float4x4(input.W0, input.W1, input.W2, float4(input.W3.xyz, 1.0f));
	//float4x4 objectworld = mul(wor, world);
	float4x4 worldViewProj = mul(world, mul(view, proj));

PS_IN output = (PS_IN)0;

output.pos = mul(input.pos, worldViewProj);
	output.tex = input.tex;

return output;
}

float4 PS( PS_IN input ) : COLOR
{
	float4 output = input.tex;
	output.a=1;
	output.r=1;
	//output = tex2D(mySampler, input.tex);
return output;
}

technique Main
{
pass P0
	{
VertexShader = compile vs_3_0 VS();
		PixelShader  = compile ps_3_0 PS();
}
}

Any help appreciated Posted Image.

Sponsor:

#2 Yura   Members   -  Reputation: 263

Like
1Likes
Like

Posted 29 December 2012 - 04:35 AM

I think your problem is in VertexDeclaration.

When you are creating new VertexElement, second parameter in it's constructor is short offset - you set its wrong here

 new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
		 new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),

 

 

float4 = 4 float * 4 bites = 16

and you make offset 12 in Color declaration






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS