Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

slimdx control inside winform

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 yahastu   Members   -  Reputation: 152


Posted 03 September 2011 - 01:51 PM

I have searched but haven't been able to find any actual code examples showing how to use slimdx on a control within a windows form.

I created a simple panel control and passed the handle to slimdx.

Then I tried to adapt the slimdx tutorial code for triangle rendering onto the control by doing the following:

a) put all the global variables needed for rendering onto the main winform class
b) put all the setup code inside the winform constructor after InitializeComponent
c) made a destructor for the winform and put all the directx cleanup code there
d) made a RenderFrame() function and hooked it up with MessagePump.Run(this, RenderFrame) in the end of the winform constructor.

Problems I'm having:
1) The triangle is not visible, although the back buffer is cleared to the clear color
2) When the application exits, it throws some crash exception
3) If I click the minimize button on the winform, the computer immediately freezes and must be re manually rebooted

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

using SlimDX;
using SlimDX.D3DCompiler;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using SlimDX.Windows;
using Device = SlimDX.Direct3D11.Device;
using Resource = SlimDX.Direct3D11.Resource;

namespace MyNamespace
	public partial class frmMain : Form
    	Device device;
    	SwapChain swapChain;
    	DeviceContext context;
    	Viewport viewport;
    	RenderTargetView renderTarget;

    	ShaderSignature inputSignature;
    	VertexShader vertexShader;
    	PixelShader pixelShader;
    	DataStream vertices;
    	SlimDX.Direct3D11.Buffer vertexBuffer;
    	InputLayout layout;

    	public frmMain()

         	//this should make slimdx render onto "gPanel" control

        	var description = new SwapChainDescription()
            	BufferCount = 1,
            	Usage = Usage.RenderTargetOutput,
            	OutputHandle = gPanel.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

        	Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);

        	// create a view of our render target, which is the backbuffer of the swap chain we just created
        	using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
            	renderTarget = new RenderTargetView(device, resource);

        	// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
        	using (var factory = swapChain.GetParent<Factory>())
            	factory.SetWindowAssociation(gPanel.Handle, WindowAssociationFlags.IgnoreAltEnter);

        	// setting a viewport is required if you want to actually see anything
        	context = device.ImmediateContext;
        	viewport = new Viewport(0.0f, 0.0f, gPanel.ClientSize.Width, gPanel.ClientSize.Height);

        	// load and compile the vertex shader
        	using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            	inputSignature = ShaderSignature.GetInputSignature(bytecode);
            	vertexShader = new VertexShader(device, bytecode);

        	// load and compile the pixel shader
        	using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
            	pixelShader = new PixelShader(device, bytecode);

        	// create test vertex data, making sure to rewind the stream afterward
        	vertices = new DataStream(12 * 3, true, true);
        	vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
        	vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
        	vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
        	vertices.Position = 0;

        	// create the vertex layout and buffer
        	var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
        	layout = new InputLayout(device, inputSignature, elements);
        	vertexBuffer = new SlimDX.Direct3D11.Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

        	// configure the Input Assembler portion of the pipeline with the vertex data
        	context.InputAssembler.InputLayout = layout;
        	context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
        	context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));

        	// set the shaders

        	MessagePump.Run(this, RenderFrame);


        	// clean up all resources
        	// anything we missed will show up in the debug output

    	private void RenderFrame()
        	// clear the render target to a soothing blue
        	context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));

        	context.Draw(3, 0);
        	swapChain.Present(0, PresentFlags.None);


#2 Mike.Popoloski   Crossbones+   -  Reputation: 2941


Posted 04 September 2011 - 08:26 AM

I copied the code you've given into a new project and it worked fine for me. If you're getting an exception, you should probably look at the actual exception instead of ignoring the information it's trying to give you. Use that to track down what exactly is going wrong. Also, if your triangle is missing, try turning on the debug runtimes to see if there are any error messages, and if that doesn't help debug it with PIX.
Mike Popoloski | Journal | SlimDX

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.