Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SlimDX] DX11 Instancing


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
6 replies to this topic

#1 Matthew Meeks   Members   -  Reputation: 210

Like
0Likes
Like

Posted 29 December 2012 - 09:11 PM

Just switched from DX9 to DX11 and am attempting to instance a triangle.  Created a vertex buffer and a buffer for the instance positions.  Not sure what it is I'm doing incorrectly.

 

Input Layout:

            var bytecode = ShaderBytecode.CompileFromFile("Instance.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None, null, includeFX);
            var effect = new Effect(device, bytecode);
            var technique = effect.GetTechniqueByIndex(0);
            var pass = technique.GetPassByIndex(0);

            layout = new InputLayout(device, pass.Description.Signature, new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0, InputClassification.PerVertexData, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1)
            });

 

Vertex Buffer:

            var stream = new DataStream(3 * 8 * 4, true, true);
            stream.Write(new Vector4( 0.2f,  0.0f,  0.0f,  1.0f));
            stream.Write(new Vector4( 0.0f,  1.0f,  0.0f,  1.0f));

            stream.Write(new Vector4(-0.2f,  0.0f,  0.0f,  1.0f));
            stream.Write(new Vector4( 0.0f,  1.0f,  0.0f,  1.0f));

            stream.Write(new Vector4( 0.0f,  2.0f,  0.0f,  1.0f));
            stream.Write(new Vector4( 0.0f,  1.0f,  0.0f,  1.0f));

            stream.Position = 0;

            vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = 3 * 8 * 4,
                Usage = ResourceUsage.Default
            });

            stream.Dispose();

 

 

Instanced Positions Buffer:

            SlimDX.Direct3D11.Buffer ipos;

            var stream2 = new DataStream(4 * 4 * 4, true, true);

            stream2.Write(new Vector4(0, 0, 0, 1));
            stream2.Write(new Vector4(2, 2, 50, 1));
            stream2.Write(new Vector4(3, 5, 20, 1));
            stream2.Write(new Vector4(4, 0, 3, 1));

            stream2.Position = 0;

            ipos = new SlimDX.Direct3D11.Buffer(device, stream2, new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = 4 * 4 * 4,
                Usage = ResourceUsage.Default
            });

            stream2.Dispose();

 

 

Draw Code:

                effect.GetVariableByName("World").AsMatrix().SetMatrix(World);
                effect.GetVariableByName("View").AsMatrix().SetMatrix(View);
                effect.GetVariableByName("Projection").AsMatrix().SetMatrix(Projection);

                device.ImmediateContext.InputAssembler.InputLayout = layout;
                device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

                device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 8 * 4, 0));
                device.ImmediateContext.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(ipos, 4 * 4, 0));

                pass.Apply(device.ImmediateContext);
                device.ImmediateContext.DrawInstanced(3, 4, 0, 0);

 

Shaders:

float4x4 World;
float4x4 View;
float4x4 Projection;

struct VS_INPUT
{
	float4 position : POSITION;
	float4 xColor : COLOR0;
	//float2 UV: TEXCOORD0;
	float4 iposition : TEXCOORD0;
};

struct PS_INPUT
{
	float4 Position : POSITION;
	float4 xColor : COLOR0;
};

PS_INPUT VS_Shader( VS_INPUT input )
{
	PS_INPUT output;
	output.Position = (input.position) + input.iposition;
	float4 worldPosition = mul(output.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
	output.xColor = input.xColor;
	output.xColor.a = 1;
	return output;
}

float4 PS_Shader( PS_INPUT input ) : SV_Target
{
	return input.xColor;
}

technique11 Render
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_5_0, VS_Shader() ) );
		SetPixelShader( CompileShader( ps_5_0, PS_Shader() ) );
	}
}

 

 

 

When the code is run, nothing is drawn.  Any help is appreciated.



Sponsor:

#2 NewDisplayName   Members   -  Reputation: 359

Like
0Likes
Like

Posted 29 December 2012 - 10:34 PM

I haven't tried out your code because of the lack of a working dev machine nearby, but I can sense that there is a problem with the way you are using instancing.

But before you directly jump to your instance buffer debugging, I would recommend to try out your shader with a basic draw primitive approach.

Basically, break down the problem into parts and debug each. But if you're short on time then go right ahead and check your instance buffer. Hope that helps.



#3 unbird   Crossbones+   -  Reputation: 6012

Like
1Likes
Like

Posted 30 December 2012 - 06:04 AM

D3D11: ERROR: ID3D11DeviceContext::DrawInstanced: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT ]
Meaning, you need SV_POSITION at your rasterizer stage:
struct PS_INPUT
{
float4 Position : SV_POSITION;
float4 xColor : COLOR0;
};


#4 Matthew Meeks   Members   -  Reputation: 210

Like
0Likes
Like

Posted 30 December 2012 - 07:48 AM

Oh, stupid mistake. Thanks smile.png



#5 Clutchplate   Members   -  Reputation: 128

Like
0Likes
Like

Posted 05 December 2013 - 06:44 PM

D3D11: ERROR: ID3D11DeviceContext::DrawInstanced: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT ]
Meaning, you need SV_POSITION at your rasterizer stage:
struct PS_INPUT
{
float4 Position : SV_POSITION;
float4 xColor : COLOR0;
};

How did you get that output? All I ever get are totally generic E_FAIL or E_INVALIDARG errors with no clue in the output as to what's wrong..



#6 ericrrichards22   Members   -  Reputation: 813

Like
0Likes
Like

Posted 05 December 2013 - 07:06 PM

Unbird, I too am curious how you managed to actually get useful error messages out of DirectX.


Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22


#7 unbird   Crossbones+   -  Reputation: 6012

Like
0Likes
Like

Posted 06 December 2013 - 05:02 AM

So let me redeem you from your pain wink.png

Create your D3D11 device with D3D11_CREATE_DEVICE_DEBUG, for SlimDX it's DeviceCreationFlags.Debug.

These messages will then turn up in the debug output log of Visual Studio. For managed languages one needs to enable native code debugging ( see here). Alternatively (I use VS 2010 Express version, so that's what I have to do) one can grab debug messages globally using DebugView.




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