void UpdateLayout(SharpDX.ViewportF viewport, SharpDX.Direct3D11.Texture2DDescription newBackBuffer) { InventoryButton.Bounds = new UniRectangle(20, viewport.Height - 90, 64, 64); CraftingButton.Bounds = new UniRectangle(104, viewport.Height - 84, 64, 64); _inventoryLabel.Bounds = new UniRectangle(47, viewport.Height - 30, 20, 20); _craftingLabel.Bounds = new UniRectangle(134, viewport.Height - 30, 20, 20); }
void PopulateCommandList(Pipeline.Pipeline pipeline, Pipeline.PipelineAssets pipelineAssets) { var viewport = new SharpDX.ViewportF(0, 0, pipeline.Size.Width, pipeline.Size.Height); var scissorRectangle = new SharpDX.Rectangle(0, 0, pipeline.Size.Width, pipeline.Size.Height); pipeline.CommandAllocators[pipeline.FrameIndex].Reset(); pipelineAssets.CommandList.Reset(pipeline.CommandAllocators[pipeline.FrameIndex], pipelineAssets.PipelineState); pipelineAssets.CommandList.SetGraphicsRootSignature(pipelineAssets.RootSignature); pipelineAssets.CommandList.SetDescriptorHeaps(1, new SharpDX.Direct3D12.DescriptorHeap[] { pipelineAssets.ConstantBufferViewHeap }); pipelineAssets.CommandList.SetGraphicsRootDescriptorTable(0, pipelineAssets.ConstantBufferViewHeap.GPUDescriptorHandleForHeapStart); pipelineAssets.CommandList.SetViewport(viewport); pipelineAssets.CommandList.SetScissorRectangles(scissorRectangle); pipelineAssets.CommandList.ResourceBarrierTransition(pipeline.RenderTargets[pipeline.FrameIndex], SharpDX.Direct3D12.ResourceStates.Present, SharpDX.Direct3D12.ResourceStates.RenderTarget); var rtvHandle = pipeline.RenderTargetViewHeap.CPUDescriptorHandleForHeapStart + pipeline.FrameIndex * pipeline.RtvDescriptorSize; pipelineAssets.CommandList.SetRenderTargets(rtvHandle, null); pipelineAssets.CommandList.ClearRenderTargetView(rtvHandle, GraphicsSettings.ClearColor.Value, 0, null); pipelineAssets.CommandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; pipelineAssets.CommandList.SetVertexBuffer(0, pipelineAssets.VertexBufferView); pipelineAssets.CommandList.DrawInstanced(3, 1, 0, 0); pipelineAssets.CommandList.ResourceBarrierTransition(pipeline.RenderTargets[pipeline.FrameIndex], SharpDX.Direct3D12.ResourceStates.RenderTarget, SharpDX.Direct3D12.ResourceStates.Present); pipelineAssets.CommandList.Close(); }
/// <summary> /// Get the array of {{viewports}} bound to the {{rasterizer stage}} /// </summary> /// <returns>An array of viewports (see <see cref="SharpDX.ViewportF"/>).</returns> /// <unmanaged>void RSGetViewports([InOut] int* NumViewports,[Out, Buffer, Optional] D3D10_VIEWPORT* pViewports)</unmanaged> /// <msdn-id>ff476477</msdn-id> /// <unmanaged>void ID3D11DeviceContext::RSGetViewports([InOut] unsigned int* pNumViewports,[Out, Buffer, Optional] D3D11_VIEWPORT* pViewports)</unmanaged> /// <unmanaged-short>ID3D11DeviceContext::RSGetViewports</unmanaged-short> public SharpDX.ViewportF[] GetViewports() { int numViewports = 0; GetViewports(ref numViewports, null); var viewports = new SharpDX.ViewportF[numViewports]; GetViewports(ref numViewports, viewports); return viewports; }
/// <summary> /// Get the array of {{viewports}} bound to the {{rasterizer stage}} /// </summary> /// <returns>An array of viewports (see <see cref="SharpDX.ViewportF"/>).</returns> /// <unmanaged>void RSGetViewports([InOut] int* NumViewports,[Out, Buffer, Optional] D3D10_VIEWPORT* pViewports)</unmanaged> /// <msdn-id>ff476477</msdn-id> /// <unmanaged>void ID3D11DeviceContext::RSGetViewports([InOut] unsigned int* pNumViewports,[Out, Buffer, Optional] D3D11_VIEWPORT* pViewports)</unmanaged> /// <unmanaged-short>ID3D11DeviceContext::RSGetViewports</unmanaged-short> public SharpDX.ViewportF[] GetViewports() { int numViewports = 0; GetViewports(ref numViewports, null); var viewports = new SharpDX.ViewportF[numViewports]; GetViewports(ref numViewports, viewports); return(viewports); }
public void PropertyStasher_pushModifyAndPopViewportAndObjectTWorld_bothAreRestored() { var context = new OperatorPartContext(); var viewport = new SharpDX.ViewportF(0, 0, 320, 200); context.Viewport = viewport; using (new PropertyStasher <OperatorPartContext>(context, "Viewport", "ObjectTWorld")) { context.Viewport = new SharpDX.ViewportF(0, 0, 640, 480); context.ObjectTWorld = SharpDX.Matrix.OrthoLH(1.0f, 1.0f, 0.1f, 1.0f); } Assert.AreEqual(SharpDX.Matrix.Identity, context.ObjectTWorld); Assert.AreEqual(viewport, context.Viewport); }
public void PropertyStasher_modifyCascadedProperty_isRestoredToInitialValue() { var context = new OperatorPartContext(); var viewport = new SharpDX.ViewportF(0, 0, 320, 200); context.Viewport = viewport; using (new PropertyStasher <OperatorPartContext>(context, "Viewport")) { var viewport2 = new SharpDX.Viewport(0, 0, 640, 480); context.Viewport = viewport2; using (new PropertyStasher <OperatorPartContext>(context, "Viewport")) { context.Viewport = new SharpDX.Viewport(0, 0, 1024, 768); } Assert.AreEqual(viewport2, context.Viewport); } Assert.AreEqual(viewport, context.Viewport); }
/// <summary> /// Create all view resources. /// </summary> Tuple <D3D11.Texture2D, D3D11.RenderTargetView, D3D11.Texture2D, D3D11.DepthStencilView, SharpDX.ViewportF, Size2, DpiScaling> IRenderLoopHost.OnRenderLoop_CreateViewResources(EngineDevice engineDevice) { m_backBufferMultisampled = null; Size2 viewSize = GetTargetRenderPixelSize(); // Create the SwapChain and associate it with the SwapChainBackgroundPanel m_swapChain = GraphicsHelper.CreateSwapChainForComposition(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_targetPanel.SwapChain = m_swapChain; m_compositionScaleChanged = true; // Get the backbuffer from the SwapChain m_backBuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(m_swapChain, 0); // Define the render target (in case of multisample an own render target) D3D11.Texture2D backBufferForRenderloop = null; if (m_renderLoop.ViewConfiguration.AntialiasingEnabled) { m_backBufferMultisampled = GraphicsHelper.CreateRenderTargetTexture(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_renderTargetView = new D3D11.RenderTargetView(engineDevice.DeviceD3D11, m_backBufferMultisampled); backBufferForRenderloop = m_backBufferMultisampled; } else { m_renderTargetView = new D3D11.RenderTargetView(engineDevice.DeviceD3D11, m_backBuffer); backBufferForRenderloop = m_backBuffer; } //Create the depth buffer m_depthBuffer = GraphicsHelper.CreateDepthBufferTexture(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_renderTargetDepth = new D3D11.DepthStencilView(engineDevice.DeviceD3D11, m_depthBuffer); //Define the viewport for rendering SharpDX.ViewportF viewPort = GraphicsHelper.CreateDefaultViewport(viewSize.Width, viewSize.Height); m_lastRefreshTargetSize = new Size(viewSize.Width, viewSize.Height); DpiScaling dpiScaling = new DpiScaling(); dpiScaling.DpiX = (float)(96.0 * m_targetPanel.CompositionScaleX); dpiScaling.DpiY = (float)(96.0 * m_targetPanel.CompositionScaleY); return(Tuple.Create(backBufferForRenderloop, m_renderTargetView, m_depthBuffer, m_renderTargetDepth, viewPort, viewSize, dpiScaling)); }
/// <summary> /// Create all view resources. /// </summary> private Tuple <D3D11.Texture2D, D3D11.RenderTargetView, D3D11.Texture2D, D3D11.DepthStencilView, SharpDX.ViewportF, Size2> OnRenderLoopCreateViewResources(EngineDevice engineDevice) { Size2 viewSize = GetTargetRenderPixelSize(); // Create the backbuffer m_backBuffer = GraphicsHelper.CreateRenderTargetTexture(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_backBufferSynchronizing = GraphicsHelper.CreateRenderTargetTexture(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_renderTargetView = new D3D11.RenderTargetView(engineDevice.DeviceD3D11, m_backBuffer); //Create the depth buffer m_depthBuffer = GraphicsHelper.CreateDepthBufferTexture(engineDevice, viewSize.Width, viewSize.Height, m_renderLoop.ViewConfiguration); m_renderTargetDepth = new D3D11.DepthStencilView(engineDevice.DeviceD3D11, m_depthBuffer); //Define the viewport for rendering SharpDX.ViewportF viewPort = GraphicsHelper.CreateDefaultViewport(viewSize.Width, viewSize.Height); m_lastRefreshTargetSize = viewSize; return(Tuple.Create(m_backBuffer, m_renderTargetView, m_depthBuffer, m_renderTargetDepth, viewPort, viewSize)); }
protected virtual void CreateSizeDependentResources(object sender, RenderEventArgs e) { var d3dDevice = DirectX.Direct3D.Device; var d3dContext = DirectX.Direct3D.Context; var d2dContext = DirectX.Direct2D.Context; bool isStereoEnabled = deviceManager.Settings.IsStereo; Cleanup(); // If the swap chain already exists, resize it. if (swapChain != null) { swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device // First, retrieve the underlying DXGI Device from the D3D Device. // Creates the swap chain using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>()) { swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc)); // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice2.MaximumFrameLatency = 1; } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. backBuffer = ToDispose(SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0)); { RenderTargetViewDescription rtvDescription = new RenderTargetViewDescription() { Dimension = RenderTargetViewDimension.Texture2DArray, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource() { MipSlice = 0, FirstArraySlice = 0, ArraySize = 1 } }; // Create a view interface on the rendertarget to use on bind. renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer, rtvDescription)); if (IsStereoEnabled) { RenderTargetViewDescription rtvDescriptionRight = new RenderTargetViewDescription() { Dimension = RenderTargetViewDimension.Texture2DArray, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource() { MipSlice = 0, FirstArraySlice = 1, ArraySize = 1 } }; renderTargetViewRight = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer, rtvDescriptionRight)); } // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = BackBuffer.Description; RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height); } // 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. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a viewport descriptor of the full window size. var viewport = new SharpDX.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f); // Set the current viewport using the descriptor. d3dContext.Rasterizer.SetViewport(viewport); // Now we set up the Direct2D render target bitmap linked to the swapchain. // Whenever we render to this bitmap, it will be directly rendered to the // swapchain associated with the window. var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1( new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), deviceManager.Dpi, deviceManager.Dpi, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw); // Direct2D needs the dxgi version of the backbuffer surface pointer. // Get a D2D surface from the DXGI back buffer to use as the D2D render target. if (IsStereoEnabled) { using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Resource1>(0)) { using (var dxgiSurface = new Surface2(dxgiBackBuffer, 0)) bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiSurface, bitmapProperties)); using (var dxgiSurface = new Surface2(dxgiBackBuffer, 1)) bitmapTargetRight = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiSurface, bitmapProperties)); } } else { using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface2>(0)) bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties)); } // So now we can set the Direct2D render target. d2dContext.Target = BitmapTarget; // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces. d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; }
private void PrepareForRendering() { // Setup states (Blend, DepthStencil, Rasterizer) if (m_blendState != null) { m_blendState.Apply(); } if (m_rasterizerState != null) { m_rasterizerState.Apply(); } if (m_depthStencilState != null) { m_depthStencilState.Apply(); } if (m_samplerState != null) { m_samplerState.Apply(); } // Build ortho-projection matrix SharpDX.ViewportF viewport = m_graphicsDevice.Viewport; float xRatio = (viewport.Width > 0) ? (1f / (viewport.Width)) : 0f; float yRatio = (viewport.Height > 0) ? (-1f / (viewport.Height)) : 0f; var matrix = new Matrix { M11 = xRatio * 2f, M22 = yRatio * 2f, M33 = 1f, M44 = 1f, M41 = -1f, M42 = 1f }; Matrix finalMatrix; Matrix.Multiply(ref m_transformMatrix, ref matrix, out finalMatrix); // Use LinearClamp for sampler state //var localSamplerState = samplerState ?? GraphicsDevice.SamplerStates.LinearClamp; // Setup effect states and parameters: SamplerState and MatrixTransform // Sets the sampler state if (m_customEffect != null) { if (m_customEffect.Technique == null) { throw new InvalidOperationException("CurrentTechnique is not set on custom effect"); } //if (customEffectSampler != null) // customEffectSampler.SetResource(localSamplerState); if (m_customEffectMatrixTransform != null) { m_customEffect.SetValue(m_customEffectMatrixTransform, finalMatrix); } } else { var spriteEffect = MyRender.GetEffect(MyEffects.SpriteBatch) as Effects.MyEffectSpriteBatchShader; spriteEffect.SetMatrixTransform(ref finalMatrix); } // Set VertexInputLayout m_graphicsDevice.VertexDeclaration = MyVertexFormatPositionTextureColor.VertexDeclaration; // VertexBuffer m_graphicsDevice.SetStreamSource(0, m_VBResourceContext.VertexBuffer, 0, MyVertexFormatPositionTextureColor.Stride); // Index buffer m_graphicsDevice.Indices = m_indexBuffer; // If this is a deferred D3D context, reset position so the first Map call will use D3D11_MAP_WRITE_DISCARD. /* if (GraphicsDevice.IsDeferred) * { * VBResourceContext.VertexBufferPosition = 0; * } */ }
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); }
void _d3DEngine_ScreenSize_Updated(SharpDX.ViewportF viewport, Texture2DDescription newBackBuffer) { _screen.Width = viewport.Width; _screen.Height = viewport.Height; }
// This method creates all application resources that depend on // the application window size. It is called at app initialization, // and whenever the application window size changes. void CreateWindowSizeDependentResources() { if (m_swapChain != null) { // If the swap chain already exists, resize it. m_swapChain.ResizeBuffers( 2, 0, 0, SharpDX.DXGI.Format.B8G8R8A8_UNorm, 0 ); } else { // If the swap chain does not exist, create it. var swapChainDesc = new SharpDX.DXGI.SwapChainDescription1 { Stereo = false, Usage = SharpDX.DXGI.Usage.RenderTargetOutput, Scaling = SharpDX.DXGI.Scaling.None, Flags = 0, }; // Use automatic sizing. swapChainDesc.Width = 0; swapChainDesc.Height = 0; // This is the most common swap chain format. swapChainDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm; // Don't use multi-sampling. swapChainDesc.SampleDescription.Count = 1; swapChainDesc.SampleDescription.Quality = 0; // Use two buffers to enable flip effect. swapChainDesc.BufferCount = 2; // We recommend using this swap effect for all applications. swapChainDesc.SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential; // Once the swap chain description is configured, it must be // created on the same adapter as the existing D3D Device. // First, retrieve the underlying DXGI Device from the D3D Device. using (var dxgiDevice = m_d3dDevice.QueryInterface <SharpDX.DXGI.Device2>()) { // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice.MaximumFrameLatency = 1; // Next, get the parent factory from the DXGI Device. using (var dxgiAdapter = dxgiDevice.Adapter) using (var dxgiFactory = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>()) // Finally, create the swap chain. using (var coreWindow = new SharpDX.ComObject(m_window)) { m_swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory , m_d3dDevice, coreWindow, ref swapChainDesc); } } } // Once the swap chain is created, create a render target view. This will // allow Direct3D to render graphics to the window. using (var backBuffer = m_swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0)) { m_renderTargetView = new SharpDX.Direct3D11.RenderTargetView(m_d3dDevice, backBuffer); // After the render target view is created, specify that the viewport, // which describes what portion of the window to draw to, should cover // the entire window. var backBufferDesc = backBuffer.Description; var viewport = new SharpDX.ViewportF { X = 0.0f, Y = 0.0f, Width = backBufferDesc.Width, Height = backBufferDesc.Height, MinDepth = 0, MaxDepth = 1, }; m_d3dDeviceContext.Rasterizer.SetViewport(viewport); } }
public void DrawPlaceMarks() { // todo11 port this Maybe instancing later? Matrix projection = Earth3d.MainWindow.RenderContext11.Projection.Matrix11; Matrix view = Earth3d.MainWindow.RenderContext11.View.Matrix11; Matrix world = Earth3d.MainWindow.RenderContext11.World.Matrix11; Matrix3d worldD = Earth3d.MainWindow.RenderContext11.World; Matrix wvp = (world * view * projection); try { Vector3 center = new Vector3(0f, 0f, 0); foreach (KmlPlacemark placemark in Placemarks) { if (placemark.ShouldDisplay()) { SharpDX.ViewportF vp = Earth3d.MainWindow.RenderContext11.ViewPort; double alt = placemark.Point.altitude + EGM96Geoid.Height(placemark.Point.latitude, placemark.Point.longitude); Vector3d point3d = Coordinates.GeoTo3dDouble(placemark.Point.latitude, placemark.Point.longitude, 1 + (alt / Earth3d.MainWindow.RenderContext11.NominalRadius)); Vector3 point = Vector3.Project(point3d.Vector311, vp.X, vp.Y, vp.Width, vp.Height, 0, 1, wvp); // point.Z = 1; KmlStyle style = placemark.Style.GetStyle(placemark.Selected); Texture11 texture = style.IconStyle.Icon.Texture; if (String.IsNullOrEmpty(style.IconStyle.Icon.Href)) { texture = Star; } double sizeFactor = 1; if (placemark.Selected) { double ticks = HiResTimer.TickCount; double elapsedSeconds = ((double)(ticks - TicksAtLastSelect)) / HiResTimer.Frequency; sizeFactor = 1 + .3 * (Math.Sin(elapsedSeconds * 15) * Math.Max(0, (1 - elapsedSeconds))); } point3d.TransformCoordinate(worldD); Vector3d dist = Earth3d.MainWindow.RenderContext11.CameraPosition - point3d; double distance = dist.Length() * Earth3d.MainWindow.RenderContext11.NominalRadius; dist.Normalize(); double dot = Vector3d.Dot(point3d, dist); // if (dot > -.2) { double baseSize = Math.Min(40, 25 * ((2 * Math.Atan(.5 * (5884764 / distance))) / .7853)) * sizeFactor; float size = (float)baseSize * style.IconStyle.Scale; //todo fix this with real centers and offset by KML data placemark.hitTestRect = new Rectangle((int)(point.X - (size / 2)), (int)(point.Y - (size / 2)), (int)(size + .5), (int)(size + .5)); if (texture != null) { center = new Vector3((float)texture.Width / 2f, (float)texture.Height / 2f, 0); Sprite2d.Draw2D(Earth3d.MainWindow.RenderContext11, texture, new SizeF(size, size), new PointF(center.X, center.Y), (float)(style.IconStyle.Heading * Math.PI / 180f), new PointF(point.X, point.Y), Color.White); } if (style.LabelStyle.Color.A > 0 && style.LabelStyle.Scale > 0) { Rectangle recttext = new Rectangle((int)(point.X + (size / 2) + 10), (int)(point.Y - (size / 2)), 1000, 100); //todo11 Earth3d.MainWindow.labelFont.DrawText(null, placemark.Name, recttext, DrawTextFormat.NoClip, style.LabelStyle.Color); } } } } } finally { } }
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); }
public static void Viewport(ref Beholder.Core.Viewport bViewport, out SharpDX.ViewportF d3dViewport) { d3dViewport = new SharpDX.ViewportF { X = bViewport.Left, Y = bViewport.Top, Width = bViewport.Width, Height = bViewport.Height, MinDepth = bViewport.MinDepth, MaxDepth = bViewport.MaxDepth }; }