private void InitializeDX() { //perform DX Device and Context initialization HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "dxPlotterElement", IntPtr.Zero); device = D3DDevice.CreateDeviceAndSwapChain(hwnd.Handle, out swapChain); deviceContext = device.GetImmediateContext(); CompositionTarget.Rendering += OnRenderingProccessReady; Texture2DDescription tdesc = new Texture2DDescription { ArraySize = 1, Width = 1, Height = 1, Format = Format.B8G8R8A8_UNORM, MipLevels = 1, SampleDescription = new SampleDescription { Count = 1, Quality = 0 }, Usage = Usage.Default, BindFlags = BindFlag.RenderTarget | BindFlag.ShaderResource, MiscFlags = ResourceMiscFlag.Shared, CpuAccessFlags = 0 }; using (Texture2D texture2D = device.CreateTexture2D(tdesc)) { renderTargetView = device.CreateRenderTargetView(texture2D); deviceContext.OM.SetRenderTargets(new RenderTargetView[] { renderTargetView }); d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface); } dxInitialized = true; OnDeviceInitialized(); }
private void InitDevice() { // device creation //device = D3DDevice.CreateDeviceAndSwapChain( // null, // DriverType.Hardware, // null, // CreateDeviceFlag.Default, // new []{FeatureLevel.FeatureLevel_10_1}, // new SwapChainDescription { // BufferCount = 1 // }, // out swapChain); device = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle, out swapChain); deviceContext = device.GetImmediateContext(); SetViews(); // Open precompiled vertex shader // This file was compiled using: fxc Render.hlsl /T vs_4_0 /EVertShader /FoRender.vs using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs")) { vertexShader = device.CreateVertexShader(stream); } deviceContext.VS.SetShader(vertexShader, null); // input layout is for the vert shader InputElementDescription inputElementDescription = new InputElementDescription(); inputElementDescription.SemanticName = "POSITION"; inputElementDescription.SemanticIndex = 0; inputElementDescription.Format = Format.R32G32B32_FLOAT; inputElementDescription.InputSlot = 0; inputElementDescription.AlignedByteOffset = 0; inputElementDescription.InputSlotClass = InputClassification.PerVertexData; inputElementDescription.InstanceDataStepRate = 0; InputLayout inputLayout; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs")) { inputLayout = device.CreateInputLayout(new [] { inputElementDescription }, stream); } deviceContext.IA.SetInputLayout(inputLayout); // Open precompiled pixel shader // This file was compiled using: fxc Render.hlsl /T ps_4_0 /EPixShader /FoRender.ps using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.ps")) { pixelShader = device.CreatePixelShader(stream); } deviceContext.PS.SetShader(pixelShader, null); // create some geometry to draw (1 triangle) SimpleVertexArray vertex = new SimpleVertexArray(); // put the vertices into a vertex buffer BufferDescription bufferDescription = new BufferDescription(); bufferDescription.Usage = Usage.Default; bufferDescription.ByteWidth = (uint)Marshal.SizeOf(vertex); bufferDescription.BindFlags = BindFlag.VertexBuffer; SubresourceData subresourceData = new SubresourceData(); IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex)); Marshal.StructureToPtr(vertex, vertexData, false); subresourceData.SysMem = vertexData; vertexBuffer = device.CreateBuffer(bufferDescription, subresourceData); deviceContext.IA.SetVertexBuffers(0, new [] { vertexBuffer }, new uint[] { 12 }, new uint[] { 0 }); deviceContext.IA.SetPrimitiveTopology(PrimitiveTopology.TriangleList); Marshal.FreeCoTaskMem(vertexData); }