protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates a SwapChain from a CoreWindow pointer SwapChainFullScreenDescription scFullScreenDesc = new SwapChainFullScreenDescription() { Windowed = deviceManager.Settings.IsWindowed, RefreshRate = new Rational(120, 1) }; #if DIRECTX11_1 return(new SwapChain1(factory, device, form.Handle, ref desc)); #else return(factory.CreateSwapChainForHwnd(device, form.Handle, ref desc, scFullScreenDesc, null)); #endif }
public void Initialize() { if (!_deviceManager.IsInitialized) { throw new InvalidOperationException("Device manager is not initialized"); } if (Initialized) { Uninitialize(); } var swapChainDescription = new SwapChainDescription1 { Width = Width, Height = Height, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(SampleCount, SampleQuality), Usage = Usage.BackBuffer | Usage.RenderTargetOutput, BufferCount = 1, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.Discard, Flags = SwapChainFlags.AllowModeSwitch }; var fullScreenDescription = new SwapChainFullScreenDescription { RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Centered, Windowed = true }; using (var dxgiDevice2 = _deviceManager.Device.QueryInterface <Device2>()) using (var dxgiFactory2 = dxgiDevice2.Adapter.GetParent <Factory2>()) { SwapChain = new SwapChain1(dxgiFactory2, _deviceManager.Device, _windowHandle, ref swapChainDescription, fullScreenDescription); dxgiFactory2.MakeWindowAssociation(_windowHandle, WindowAssociationFlags.IgnoreAll); } Texture = Resource.FromSwapChain <Texture2D>(SwapChain, 0); RenderTargetView = new RenderTargetView(_deviceManager.Device, Texture); Initialized = true; }
/// <summary> /// Creates a swapchain associated to the specified HWND. This is applicable only for Desktop platform. /// </summary> /// <param name="factory">The DXGI Factory used to create the swapchain.</param> /// <param name="device">The associated device instance.</param> /// <param name="hwnd">The HWND of the window to which this swapchain is associated.</param> /// <param name="description">The swap chain description.</param> /// <param name="fullScreenDescription">The fullscreen description of the swap chain. Default is null.</param> /// <param name="restrictToOutput">The output to which this swap chain should be restricted. Default is null, meaning that there is no restriction.</param> public SwapChain1(Factory2 factory, ComObject device, IntPtr hwnd, ref SwapChainDescription1 description, SwapChainFullScreenDescription? fullScreenDescription = null, Output restrictToOutput = null) : base(IntPtr.Zero) { factory.CreateSwapChainForHwnd(device, hwnd, ref description, fullScreenDescription, restrictToOutput, this); }
public void Initialize() { if(!_deviceManager.IsInitialized) throw new InvalidOperationException("Device manager is not initialized"); if(Initialized) Uninitialize(); var swapChainDescription = new SwapChainDescription1 { Width = Width, Height = Height, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(SampleCount, SampleQuality), Usage = Usage.BackBuffer | Usage.RenderTargetOutput, BufferCount = 1, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.Discard, Flags = SwapChainFlags.AllowModeSwitch }; var fullScreenDescription = new SwapChainFullScreenDescription { RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Centered, Windowed = true }; using(var dxgiDevice2 = _deviceManager.Device.QueryInterface<Device2>()) using(var dxgiFactory2 = dxgiDevice2.Adapter.GetParent<Factory2>()) { SwapChain = new SwapChain1(dxgiFactory2, _deviceManager.Device, _windowHandle, ref swapChainDescription, fullScreenDescription); dxgiFactory2.MakeWindowAssociation(_windowHandle, WindowAssociationFlags.IgnoreAll); } Texture = Resource.FromSwapChain<Texture2D>(SwapChain, 0); RenderTargetView = new RenderTargetView(_deviceManager.Device, Texture); Initialized = true; }
public static void Run() { var featureLevels = new[] { FeatureLevel.Level_11_1, FeatureLevel.Level_11_0 }; const DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug; const int width = 640; const int height = 480; using (var form = new RenderForm { Width = width, Height = height }) { var swapChainDescription = new SwapChainDescription1 { Width = form.ClientSize.Width, Height = form.ClientSize.Height, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(4, 4), Usage = Usage.BackBuffer | Usage.RenderTargetOutput, BufferCount = 1, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.Discard, Flags = SwapChainFlags.AllowModeSwitch }; var swapChainFullScreenDescription = new SwapChainFullScreenDescription { RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Centered, Windowed = true }; var samplerStateDescription = new SamplerStateDescription { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, Filter = Filter.MinMagMipLinear }; var rasterizerStateDescription = RasterizerStateDescription.Default(); rasterizerStateDescription.IsFrontCounterClockwise = true; // Set up the graphics devices using (var device0 = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags, featureLevels)) using (var device1 = device0.QueryInterface <SharpDX.Direct3D11.Device1>()) using (var context = device0.ImmediateContext.QueryInterface <DeviceContext1>()) // Create shaders and related resources using (var vertexShaderBytecode = ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0", ShaderFlags.Debug)) using (var vertexShader = new VertexShader(device1, vertexShaderBytecode)) using (var pixelShaderBytecode = ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0", ShaderFlags.Debug)) using (var pixelShader = new PixelShader(device1, pixelShaderBytecode)) using (var inputLayout = new InputLayout(device1, ShaderSignature.GetInputSignature(vertexShaderBytecode), new[] { new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0), })) using (var worldViewProjectionBuffer = new SharpDX.Direct3D11.Buffer(device1, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)) using (var texture = TextureLoader.CreateTexture2DFromBitmap(device1, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "text.png"))) using (ShaderResourceView textureView = new ShaderResourceView(device1, texture)) using (var samplerState = new SamplerState(device1, samplerStateDescription)) // Prepare rendering targets and related resources using (var dxgiDevice2 = device1.QueryInterface <SharpDX.DXGI.Device2>()) using (var dxgiFactory2 = dxgiDevice2.Adapter.GetParent <Factory2>()) using (var swapChain = new SwapChain1(dxgiFactory2, device1, form.Handle, ref swapChainDescription, swapChainFullScreenDescription)) using (var backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0)) using (var rasterizerState = new RasterizerState(device1, rasterizerStateDescription)) using (var renderTargetView = new RenderTargetView(device1, backBuffer)) { var viewport = new ViewportF(0, 0, backBuffer.Description.Width, backBuffer.Description.Height); context.Rasterizer.SetViewport(viewport); context.Rasterizer.State = rasterizerState; var depthBufferDescription = new Texture2DDescription { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = backBuffer.Description.Width, Height = backBuffer.Description.Height, SampleDescription = swapChain.Description.SampleDescription, BindFlags = BindFlags.DepthStencil, }; var depthStencilViewDescription = new DepthStencilViewDescription { Dimension = swapChain.Description.SampleDescription.Count > 1 || swapChain.Description.SampleDescription.Quality > 0 ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D }; var depthStencilStateDescription = new DepthStencilStateDescription { IsDepthEnabled = true, DepthComparison = Comparison.Less, DepthWriteMask = DepthWriteMask.All, IsStencilEnabled = false, StencilReadMask = 0xff, StencilWriteMask = 0xff, FrontFace = new DepthStencilOperationDescription { Comparison = Comparison.Always, PassOperation = StencilOperation.Keep, FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Increment }, BackFace = new DepthStencilOperationDescription { Comparison = Comparison.Always, PassOperation = StencilOperation.Keep, FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Decrement } }; using (var depthBuffer = new Texture2D(device1, depthBufferDescription)) using (var depthStencilView = new DepthStencilView(device1, depthBuffer, depthStencilViewDescription)) using (var depthStencilState = new DepthStencilState(device1, depthStencilStateDescription)) { context.OutputMerger.SetRenderTargets(depthStencilView, renderTargetView); context.OutputMerger.DepthStencilState = depthStencilState; context.InputAssembler.InputLayout = inputLayout; context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer); context.VertexShader.Set(vertexShader); context.PixelShader.Set(pixelShader); context.PixelShader.SetShaderResource(0, textureView); context.PixelShader.SetSampler(0, samplerState); form.Show(); var cameraPosition = new Vector3(1.5f, 1.8f, -3); var cameraTarget = Vector3.Zero; var cameraUp = Vector3.UnitY; var worldMatrix = Matrix.Identity; var viewMatrix = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUp); // reorient everything to camera space var projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 3f, form.ClientSize.Width / (float)form.ClientSize.Height, .5f, 100f); // create a generic perspective projection matrix var viewProjection = Matrix.Multiply(viewMatrix, projectionMatrix); // apply the perspective projection to the view matrix so that we're performing both operations var worldViewProjection = worldMatrix * viewProjection; // include world translation with the view projection matrix worldViewProjection.Transpose(); var model = GetCubeModel(); using (var vertexBuffer = CreateBuffer(device1, BindFlags.VertexBuffer, model.Vertices)) using (var indexBuffer = CreateBuffer(device1, BindFlags.IndexBuffer, model.Triangles)) { var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vertex>(), 0); context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); context.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0); context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer); RenderLoop.Run(form, () => { context.ClearRenderTargetView(renderTargetView, Color.CornflowerBlue); context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0); context.DrawIndexed(model.Triangles.Length * 3, 0, 0); swapChain.Present(1, PresentFlags.None); }); } } } } }