Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


[SlimDX/D3D11] Why won't my triangle draw?


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

#1 jdub   Members   -  Reputation: 419

Like
0Likes
Like

Posted 16 August 2012 - 06:39 PM

Just started learning DX11 coming from DX9. I'm trying to set up a simple app to draw a triangle on the screen. The screen clears to blue as expected but... no triangle.

public partial class Form1 : Form
    {
	    SlimDX.Direct3D11.Device device;
	    SwapChain swapChain;
	    DeviceContext context;
	    Viewport viewport;
	    RenderTargetView renderTarget;
	    PixelShader pShader;
	    VertexShader vShader;
	    public Form1()
	    {
		    var description = new SwapChainDescription()
		    {
			    BufferCount = 1,
			    Usage = Usage.RenderTargetOutput,
			    OutputHandle = this.Handle,
			    IsWindowed = true,
			    ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
			    SampleDescription = new SampleDescription(1, 0),
			    Flags = SwapChainFlags.AllowModeSwitch,
			    SwapEffect = SwapEffect.Discard
		    };
		    Result result = SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out this.device, out this.swapChain);
		    if (!result.IsSuccess)
		    {
			    throw new Exception();
		    }
		    if (!this.device.Factory.SetWindowAssociation(this.Handle, WindowAssociationFlags.None).IsSuccess)
		    {
			    throw new Exception();
		    }
		    this.context = device.ImmediateContext;
		    this.viewport = new Viewport(0.0f, 0.0f, this.ClientSize.Width, this.ClientSize.Height);
		    Resource11 rsrc = Resource11.FromSwapChain<Texture2D>(this.swapChain, 0);
		    this.renderTarget = new RenderTargetView(this.device, rsrc);
		    //rsrc.Dispose();
		    //set up shaders
		    ShaderBytecode vertexByteCode = ShaderBytecode.CompileFromFile("C:/Users/Jared/Documents/Visual Studio 2010/Projects/Dx11Test/Dx11Test/DX11Test.fx",
			    "VShader",
			    "vs_4_0",
			    ShaderFlags.None,
			    EffectFlags.None);
		    this.vShader = new VertexShader(this.device, vertexByteCode);
		    ShaderBytecode pixelByteCode = ShaderBytecode.CompileFromFile("C:/Users/Jared/Documents/Visual Studio 2010/Projects/Dx11Test/Dx11Test/DX11Test.fx",
			    "PShader",
			    "ps_4_0",
			    ShaderFlags.None,
			    EffectFlags.None);
		    this.pShader = new PixelShader(this.device, pixelByteCode);
		    //Create Vertex Buffer
		    Vector3[] vertices =
		    {
				  new Vector3(0.0f, 0.0f, 1.0f),
				  new Vector3(1.0f, 0.0f, 1.0f),
				  new Vector3(1.0f, 1.0f, 1.0f)
		    };
		    DataStream stream = new DataStream(12 * 3, true, true);
		    stream.Write<Vector3>(vertices[0]);
		    stream.Write<Vector3>(vertices[1]);
		    stream.Write<Vector3>(vertices[2]);
		    stream.Position = 0;
		    BufferDescription descr = new BufferDescription(
			    Marshal.SizeOf(typeof(Vector3)) * 3,
			    ResourceUsage.Default,
			    BindFlags.VertexBuffer,
			    CpuAccessFlags.None,
			    ResourceOptionFlags.None,
			    Marshal.SizeOf(typeof(Vector3))
			    );

		    DXBuffer buffer = new DXBuffer(this.device, stream, descr);
		    VertexBufferBinding vBuffer = new VertexBufferBinding(buffer, 12, 0);
		    //Input Assembler
		    InputElement[] inputElements = { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
		    InputLayout inputLayout = new InputLayout(this.device, ShaderSignature.GetInputSignature(vertexByteCode), inputElements);
		    this.context.InputAssembler.InputLayout = inputLayout;
		    this.context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
		    this.context.InputAssembler.SetVertexBuffers(0, vBuffer);
		    this.context.OutputMerger.SetTargets(this.renderTarget);
		    this.context.Rasterizer.SetViewports(this.viewport);
		    this.context.VertexShader.Set(this.vShader);
		    this.context.PixelShader.Set(this.pShader);
	    }
	    /// <summary>
	    /// Clean up any resources being used.
	    /// </summary>
	    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	    protected override void Dispose(bool disposing)
	    {
		    this.swapChain.Dispose();
		    this.context.Dispose();
		    this.renderTarget.Dispose();
		    base.Dispose(disposing);
	    }
	    public void Render()
	    {
		    this.context.ClearRenderTargetView(this.renderTarget, new Color4(Color.Azure.ToArgb()));
		    this.context.Draw(3, 0);
		    this.swapChain.Present(0, PresentFlags.None);
	    }
    }

J.W.

Sponsor:

#2 BCullis   Crossbones+   -  Reputation: 1813

Like
2Likes
Like

Posted 16 August 2012 - 08:07 PM

Where do you set up your view/projection matrices for the vertex translation?

What does your shader code look like?

Are you using an ortho projection matrix? Right-handed or left-handed coordinates? (Affects winding order, your triangle could be "backwards")
Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#3 jdub   Members   -  Reputation: 419

Like
0Likes
Like

Posted 17 August 2012 - 12:14 AM

Yep. was the triangle order making it get culled. Thanks for helping!
J.W.




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