/// <inveritdoc/> public override void Initialize(DeviceManager deviceManager) { base.Initialize(deviceManager); surfaceImageSourceNative.Device = Collect(DeviceManager.DeviceDirect3D.QueryInterface<SharpDX.DXGI.Device>()); }
public override void Initialize(DeviceManager deviceManager) { base.Initialize(deviceManager); // Remove previous buffer if (this.constantBuffer != null) { this.constantBuffer.Dispose(); } // RemoveAndDispose(ref constantBuffer); // Setup local variables var d3dDevice = deviceManager.DeviceDirect3D; var d3dContext = deviceManager.ContextDirect3D; var path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "HelixToolkit.UWP"); // Loads vertex shader bytecode var vertexShaderByteCode = NativeFile.ReadAllBytes(path + "\\MiniCube_VS.fxo"); this.vertexShader = new VertexShader(d3dDevice, vertexShaderByteCode); // Loads pixel shader bytecode this.pixelShader = new PixelShader(d3dDevice, NativeFile.ReadAllBytes(path + "\\MiniCube_PS.fxo")); // Layout from VertexShader input signature this.layout = new InputLayout( d3dDevice, vertexShaderByteCode, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); // Instantiate Vertex buffer from vertex data var vertices = SharpDX.Direct3D11.Buffer.Create( d3dDevice, BindFlags.VertexBuffer, new[] { new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), }); this.vertexBufferBinding = new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() * 2, 0); // Create Constant Buffer this.constantBuffer = new SharpDX.Direct3D11.Buffer( d3dDevice, Utilities.SizeOf<SharpDX.Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); this.clock = new Stopwatch(); this.clock.Start(); }
/// <summary> /// Creates the device manager and image source when the viewport is loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param> private void Viewport3DXLoaded(object sender, RoutedEventArgs e) { var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi; int pixelWidth = (int)(this.ActualWidth * logicalDpi / 96.0); int pixelHeight = (int)(this.ActualHeight * logicalDpi / 96.0); // Safely dispose any previous instance // Creates a new DeviceManager (Direct3D, Direct2D, DirectWrite, WIC) this.deviceManager = new DeviceManager(); // Use CoreWindowTarget as the rendering target (Initialize SwapChain, RenderTargetView, DepthStencilView, BitmapTarget) this.d3dTarget = new SwapChainTarget((SwapChainPanel)this.ItemsPanelRoot, pixelWidth, pixelHeight); this.deviceManager.OnInitialize += this.d3dTarget.Initialize; this.deviceManager.OnInitialize += this.Initialize; this.d3dTarget.OnRender += this.Render; // Initialize the device manager and all registered deviceManager.OnInitialize this.deviceManager.Initialize(DisplayInformation.GetForCurrentView().LogicalDpi); // Setup rendering callback CompositionTarget.Rendering += this.CompositionTargetRendering; // Callback on DpiChanged DisplayProperties.LogicalDpiChanged += this.DisplayPropertiesLogicalDpiChanged; }
/// <summary> /// Initializes the model. /// </summary> /// <param name="deviceManager">The device manager.</param> private void Initialize(DeviceManager deviceManager) { foreach (Element3D e in this.Items) { e.Initialize(deviceManager); } }
/// <summary> /// Initializes the element. /// </summary> /// <param name="deviceManager">The device manager.</param> public virtual void Initialize(DeviceManager deviceManager) { }
private void devices_OnDpiChanged(DeviceManager obj) { if (OnSizeChanged != null) OnSizeChanged(this); }
/// <summary> /// Initializes this instance. /// </summary> /// <param name="deviceManager">The device manager</param> public virtual void Initialize(DeviceManager deviceManager) { this.DeviceManager = deviceManager; // If the DPI is changed, we need to perform a OnSizeChanged event deviceManager.OnDpiChanged -= devices_OnDpiChanged; deviceManager.OnDpiChanged += devices_OnDpiChanged; }
public override void Initialize(DeviceManager deviceManager) { base.Initialize(deviceManager); // Save the context instance this.deviceContext = deviceManager.DeviceDirect3D.ImmediateContext1; // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size. float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f; // Properties of the swap chain global::SharpDX.DXGI.SwapChainDescription1 swapChainDescription = new global::SharpDX.DXGI.SwapChainDescription1() { // No transparency. AlphaMode = global::SharpDX.DXGI.AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = global::SharpDX.DXGI.Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(this.swapChainPanel.RenderSize.Height * pixelScale), Width = (int)(this.swapChainPanel.RenderSize.Width * pixelScale), // Default multisampling. SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = global::SharpDX.DXGI.Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = global::SharpDX.DXGI.SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = global::SharpDX.DXGI.Usage.BackBuffer | global::SharpDX.DXGI.Usage.RenderTargetOutput, }; // Retrive the DXGI device associated to the Direct3D device. using (global::SharpDX.DXGI.Device3 dxgiDevice3 = deviceManager.DeviceDirect3D.QueryInterface <global::SharpDX.DXGI.Device3>()) { // Get the DXGI factory automatically created when initializing the Direct3D device. using (global::SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <global::SharpDX.DXGI.Factory3>()) { // Create the swap chain and get the highest version available. using (global::SharpDX.DXGI.SwapChain1 swapChain1 = new global::SharpDX.DXGI.SwapChain1(dxgiFactory3, deviceManager.DeviceDirect3D, ref swapChainDescription)) { this.swapChain = swapChain1.QueryInterface <global::SharpDX.DXGI.SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (global::SharpDX.DXGI.ISwapChainPanelNative nativeObject = global::SharpDX.ComObject.As <global::SharpDX.DXGI.ISwapChainPanelNative>(this.swapChainPanel)) { // Set its swap chain. nativeObject.SwapChain = this.swapChain; } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. // TODO: Recreate a DepthStencilBuffer is inefficient. We should only have one depth buffer. Shared depth buffer? using (var depthBuffer = new global::SharpDX.Direct3D11.Texture2D(DeviceManager.DeviceDirect3D, new global::SharpDX.Direct3D11.Texture2DDescription() { Format = global::SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)swapChainDescription.Width, Height = (int)swapChainDescription.Height, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), BindFlags = global::SharpDX.Direct3D11.BindFlags.DepthStencil, })) this.depthStencilView = Collect(new global::SharpDX.Direct3D11.DepthStencilView(DeviceManager.DeviceDirect3D, depthBuffer, new global::SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = global::SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a Texture2D from the existing swap chain to use as this.backBuffer = global::SharpDX.Direct3D11.Texture2D.FromSwapChain <global::SharpDX.Direct3D11.Texture2D>(this.swapChain, 0); this.renderTargetView = new global::SharpDX.Direct3D11.RenderTargetView(deviceManager.DeviceDirect3D, this.backBuffer); var viewport = new global::SharpDX.ViewportF(0, 0, (float)swapChainDescription.Width, (float)swapChainDescription.Height, 0.0f, 1.0f); RenderTargetBounds = new Rect(viewport.X, viewport.Y, viewport.Width, viewport.Height); //DeviceManager.ContextDirect2D.Target = this.backBuffer.as; DeviceManager.ContextDirect3D.Rasterizer.SetViewport(viewport); }
public override void Initialize(DeviceManager deviceManager) { base.Initialize(deviceManager); // Save the context instance this.deviceContext = deviceManager.DeviceDirect3D.ImmediateContext1; // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size. float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f; // Properties of the swap chain SharpDX.DXGI.SwapChainDescription1 swapChainDescription = new SharpDX.DXGI.SwapChainDescription1() { // No transparency. AlphaMode = SharpDX.DXGI.AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(this.swapChainPanel.RenderSize.Height * pixelScale), Width = (int)(this.swapChainPanel.RenderSize.Width * pixelScale), // Default multisampling. SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = SharpDX.DXGI.Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput, }; // Retrive the DXGI device associated to the Direct3D device. using (SharpDX.DXGI.Device3 dxgiDevice3 = deviceManager.DeviceDirect3D.QueryInterface<SharpDX.DXGI.Device3>()) { // Get the DXGI factory automatically created when initializing the Direct3D device. using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent<SharpDX.DXGI.Factory3>()) { // Create the swap chain and get the highest version available. using (SharpDX.DXGI.SwapChain1 swapChain1 = new SharpDX.DXGI.SwapChain1(dxgiFactory3, deviceManager.DeviceDirect3D, ref swapChainDescription)) { this.swapChain = swapChain1.QueryInterface<SharpDX.DXGI.SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = SharpDX.ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(this.swapChainPanel)) { // Set its swap chain. nativeObject.SwapChain = this.swapChain; } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. // TODO: Recreate a DepthStencilBuffer is inefficient. We should only have one depth buffer. Shared depth buffer? using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(DeviceManager.DeviceDirect3D, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)swapChainDescription.Width, Height = (int)swapChainDescription.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) this.depthStencilView = Collect(new SharpDX.Direct3D11.DepthStencilView(DeviceManager.DeviceDirect3D, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a Texture2D from the existing swap chain to use as this.backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(this.swapChain, 0); this.renderTargetView = new SharpDX.Direct3D11.RenderTargetView(deviceManager.DeviceDirect3D, this.backBuffer); var viewport = new SharpDX.ViewportF(0, 0, (float)swapChainDescription.Width, (float)swapChainDescription.Height, 0.0f, 1.0f); RenderTargetBounds = new Rect(viewport.X, viewport.Y, viewport.Width, viewport.Height); //DeviceManager.ContextDirect2D.Target = this.backBuffer.as; DeviceManager.ContextDirect3D.Rasterizer.SetViewport(viewport); }