/// <summary> /// Initialize for on-screen rendering /// </summary> public void Initialize(IntPtr outputHandle) { Debug.Assert(Initialized == false); if (UseMsaa) { InitializeDevice().Dispose(); } _swapChainDescription = new SwapChainDescription { BufferCount = 2, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = outputHandle, SampleDescription = SampleDescription, SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device device; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, _swapChainDescription, out device, out _swapChain); SetDeviceContextHolder(new DeviceContextHolder(device)); using (var factory = _swapChain.GetParent <Factory>()) { factory.SetWindowAssociation(outputHandle, WindowAssociationFlags.IgnoreAll); } InitializeInner(); Initialized = true; }
protected override void Initialize(DisplayWindowConfiguration displayWindowConfiguration) { // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(displayWindowConfiguration.Width, displayWindowConfiguration.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new [] { FeatureLevel.Level_10_0 }, desc, out _device, out _swapChain); // Ignore all windows events Factory factory = _swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer _backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0); _backBufferView = new RenderTargetView(_device, _backBuffer); }
public B3DWindow(string name, int x, int y) { var form = new RenderForm(name); form.ClientSize = new Size(x, y); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; swChainDesc = desc; Device device; SwapChain swapChain; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain); var context = device.ImmediateContext; // Ignore all windows events var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); dxgiFactory = factory; ctx = context; wndForm = form; }
public IGraphicsDevice CreateDevice(DotGame.Graphics.DeviceCreationFlags flags) { if (graphicsDevice != null) { return(graphicsDevice); } SwapChainDescription swapChainDescription = new SwapChainDescription() { BufferCount = 1, Flags = SwapChainFlags.AllowModeSwitch, IsWindowed = true, ModeDescription = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device device; Device.CreateWithSwapChain(DriverType.Hardware, EnumConverter.Convert(flags), swapChainDescription, out device, out swapChain); Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); graphicsDevice = new GraphicsDevice(this, device, swapChain, flags); this.Show(); return(graphicsDevice); }
protected override void Initialize(Configuration configuration) { var desc = new SwapChainDescription { BufferCount = 1, //ModeDescription = new ModeDescription(configuration.Width, configuration.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.ShaderInput, Flags = SwapChainFlags.None }; //SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { FeatureLevel.Level_10_0 }, desc, out _device, out _swapChain); // above line is expanded to... Device = new Device( DriverType.Hardware, DeviceCreationFlags.BgraSupport #if DEBUG //| DeviceCreationFlags.Debug #endif , FeatureLevel.Level_10_0); using (Factory1 factory1 = new Factory1()) SwapChain = new SwapChain(factory1, Device, desc); // ...this! var factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll); BackBuffer = Resource.FromSwapChain <Texture2D>(SwapChain, 0); RenderView = new RenderTargetView(Device, BackBuffer); }
protected void InitializeDeviceResources() { var backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm); SwapChainDescriptor = new SwapChainDescription() { BufferCount = 1, ModeDescription = backBufferDesc, IsWindowed = true, OutputHandle = RenderForm.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, Direct3D11.DeviceCreationFlags.BgraSupport, SwapChainDescriptor, out GameDevice, out SwapChain); DeviceContext = GameDevice.ImmediateContext; Viewport = new Viewport(0, 0, Width, Height, 0.0f, 1.0f); DeviceContext.Rasterizer.SetViewport(Viewport); Factory = SwapChain.GetParent <SharpDX.DXGI.Factory>(); //factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll); BackBufer = SwapChain.GetBackBuffer <Direct3D11.Texture2D>(0); RenderTargetView = new RenderTargetView(GameDevice, BackBufer); }
protected void OnInitializeDevice() { Form.ClientSize = new Size(_width, _height); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(_width, _height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = Form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; // Create Device and SwapChain Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out _device, out _swapChain); outputMerger = _device.OutputMerger; inputAssembler = _device.InputAssembler; Factory factory = _swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Form.Handle, WindowAssociationFlags.None); }
private void InitDeviceAndSwapChain(SwapChainDescription currentDescription) { DeviceCreationFlags flags = DeviceCreationFlags.None; #if DEBUG // flags |= DeviceCreationFlags.Debug; #endif flags |= DeviceCreationFlags.SingleThreaded; DriverType type; #if SOFTWARE type = DriverType.Warp; #else type = DriverType.Hardware; #endif Device.CreateWithSwapChain(null, type, flags, currentDescription, out device, out swapChain); // Stops Alt + Enter from causing fullscreen skrewiness. using (Factory factory = swapChain.GetParent <Factory>()) { factory.SetWindowAssociation(renderFrame.Handle, WindowAssociationFlags.IgnoreAltEnter); } }
public void CreateWithSwapChain(IntPtr handle, int width, int height, bool windowed = true, bool allowModeSwitch = false) { m_swapchainDesc = new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = windowed, OutputHandle = handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.ShaderInput, Flags = allowModeSwitch ? SwapChainFlags.AllowModeSwitch : SwapChainFlags.None }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, m_swapchainDesc, out m_device, out m_swapchain); m_context = m_device.ImmediateContext; var factory = m_swapchain.GetParent <Factory>(); factory.MakeWindowAssociation(handle, WindowAssociationFlags.IgnoreAll); factory.Dispose(); //first resetbuffer m_resizeWidth = width; m_resizeHeight = height; DoResizeBuffer(); }
/// <summary> /// Create required DirectX resources. /// Derived calls should begin with base.InternalInitialize() /// </summary> protected virtual void InternalInitialize() { var dpiScale = GetDpiScale(); SurfaceWidth = (int)(ActualWidth < 0 ? 0 : Math.Ceiling(ActualWidth * dpiScale)); SurfaceHeight = (int)(ActualHeight < 0 ? 0 : Math.Ceiling(ActualHeight * dpiScale)); var swapChainDescription = new SwapChainDescription { OutputHandle = Hwnd, BufferCount = 1, Flags = SwapChainFlags.AllowModeSwitch, IsWindowed = true, ModeDescription = new ModeDescription(SurfaceWidth, SurfaceHeight, new Rational(60, 1), Format.B8G8R8A8_UNorm), SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.Shared }; SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, swapChainDescription, out _device, out _swapChain); // Ignore all windows events using (var factory = _swapChain.GetParent <Factory>()) { factory.MakeWindowAssociation(Hwnd, WindowAssociationFlags.IgnoreAll); } // New RenderTargetView from the backbuffer _backBuffer = Resource.FromSwapChain <Texture2D>(_swapChain, 0); _renderTargetView = new RenderTargetView(_device, _backBuffer); }
private void CreateDeviceAndSwapChain() { var desc = new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(_width, _height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = Form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, Flags = SwapChainFlags.AllowModeSwitch }; Device _device; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.SingleThreaded, desc, out _device, out _swapChain); Device = _device; _immediateContext = Device.ImmediateContext; outputMerger = _immediateContext.OutputMerger; inputAssembler = _immediateContext.InputAssembler; Factory factory = _swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Form.Handle, WindowAssociationFlags.None); }
public TriForm() { InitializeComponent(); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(this.ClientSize.Width, this.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = this.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain); context = device.ImmediateContext; // Ignore all windows events factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(this.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); // Compile Vertex and Pixel shaders vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None); vertexShader = new VertexShader(device, vertexShaderByteCode); pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None); pixelShader = new PixelShader(device, pixelShaderByteCode); // Layout from VertexShader input signature layout = new InputLayout( device, ShaderSignature.GetInputSignature(vertexShaderByteCode), new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); // Instantiate Vertex buiffer from vertex data vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[] { // Position // Color new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }); // Prepare All the stages context.InputAssembler.InputLayout = layout; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0)); context.VertexShader.Set(vertexShader); context.Rasterizer.SetViewport(new Viewport(0, 0, this.ClientSize.Width, this.ClientSize.Height, 0.0f, 1.0f)); context.PixelShader.Set(pixelShader); context.OutputMerger.SetTargets(renderView); }
public Game() { this.form = new RenderForm("Mallos.Input.Test"); this.renderLoop = new RenderLoop(form); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); renderTarget = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderTargetView = new RenderTargetView(device, renderTarget); InitDemoResources(); }
public Renderer(int Width, int Height, IntPtr? OutputHandle) { if (Width < 1) Width = 1; if (Height < 1) Height = 1; bufferWidth = Width; bufferHeight = Height; deviceUsers++; if (device == null) device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, FeatureLevel.Level_11_0); if (OutputHandle.HasValue) { SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(BufferWidth, BufferHeight, new Rational(120, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = OutputHandle.Value, SampleDescription = BufferSampleDescription, SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, Flags = SwapChainFlags.AllowModeSwitch, }; swapChain = new SwapChain(device.Factory, Device, swapChainDesc); using (var factory = swapChain.GetParent<Factory>()) factory.SetWindowAssociation(OutputHandle.Value, WindowAssociationFlags.IgnoreAltEnter); } LightingSystem = new ForwardLighting(this); SetupRenderTargets(); LightingSystem.Initialize(); }
/// <summary> /// 类初始化函数 /// </summary> /// <param name="formName"></param> public Direct2D(string formName = "Default") { _keybord = new Keyboard(new DirectInput()); _keybord.Acquire(); RenderQuene = new List <RenderQuene_2D>(); Resources = new List <Resource_2D>(); Mainform = new DrawFrom(formName); //创建一个渲染窗口 Mainform.AllowUserResizing = false; //设置不可调整大小 Mainform.KeyCallBackSet(KeyBordCallBack); //设置键盘回调 Mainform.MouseCallBackSet(MouseCallBack); //设置鼠标回调 CreateSwapChain(CreateDesc(Mainform)); //创建设备和交换链 D2dFactory = new SharpDX.Direct2D1.Factory(); //Direct2D1.Factory 是D2D渲染工厂 FactoryDWrite = new SharpDX.DirectWrite.Factory(); //RW工厂 DXGIFactory = SwapChain.GetParent <SharpDX.DXGI.Factory>(); //DXGI.Factory 是基础渲染工厂 DXGIFactory.MakeWindowAssociation(Mainform.Handle, WindowAssociationFlags.IgnoreAll); //关联窗口 并忽略所有事件 // 从backbuffer新建renderTargetView BackBuffer = Texture2D.FromSwapChain <Texture2D>(SwapChain, 0); BackBufferView = new RenderTargetView(D2Ddevice, BackBuffer); using (Surface surface = BackBuffer.QueryInterface <Surface>()) { D2dRenderTarget = new RenderTarget(D2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); } DefaultBrush = new SolidColorBrush(D2dRenderTarget, Color.White); }
/// <summary> /// Initializes all the DirectX boilerplate required to render models. /// </summary> /// <param name="windowName">The name of the application window.</param> /// <param name="windowWidth">Window width in pixels.</param> /// <param name="windowHeight">Window height in pixels.</param> /// <param name="fps">Target frames per second.</param> public Renderer(string windowName, int windowWidth, int windowHeight, int fps) { form = new RenderForm(windowName); form.Width = windowWidth; form.Height = windowHeight; swapChainDescription = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.Width, form.Height, new Rational(fps, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDescription, out device, out swapChain); context = device.ImmediateContext; // Ignore all windows events factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); // Create render view and depth buffer backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); var depthBuffer = new Texture2D(device, new Texture2DDescription() { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = form.Width, Height = form.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); depthView = new DepthStencilView(device, depthBuffer); var viewport = new RawViewportF(); viewport.Width = form.Width; viewport.Height = form.Height; viewport.X = 0; viewport.Y = 0; viewport.MinDepth = 0f; viewport.MaxDepth = 1f; context.Rasterizer.SetViewport(viewport); context.OutputMerger.SetTargets(depthView, renderView); }
public void InitRendering() { try { lock (_drawLock) { m_Ready = false; ResizeRedraw = true; var desc = new SwapChainDescription { BufferCount = 2, ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.Shared }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { SharpDX.Direct3D.FeatureLevel.Level_9_3 }, desc, out device, out swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); var bitmapProperties = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore)); clientArea = new RawRectangleF { Left = 0, Top = 0, Bottom = ClientSize.Height, Right = ClientSize.Width }; factory.Dispose(); backBuffer.Dispose(); m_Ready = true; } } catch (Exception ee) { } }
public Graphics() { RenderForm = new RenderForm("Apple & Cinnamon") { Width = (int)(Screen.PrimaryScreen.Bounds.Width * ScreenSizeScale), Height = (int)(Screen.PrimaryScreen.Bounds.Height * ScreenSizeScale), StartPosition = FormStartPosition.CenterScreen }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(RenderForm.ClientSize.Width, RenderForm.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = RenderForm.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }, out var device, out var swapChain); Device = device; SwapChain = swapChain; D2dFactory = new Factory(); D2DeviceContext = new _d2d.DeviceContext3( new _d2d.Device3(new Factory().QueryInterface <_d2d.Factory4>(), device.QueryInterface <_dxgi.Device>()), DeviceContextOptions.None); D2DeviceContext.Target = D2DeviceContext.CreateBitmapRenderTarget(swapChain); SpriteBatch = new SpriteBatch(D2DeviceContext); SwapChain.GetParent <SharpDX.DXGI.Factory>().MakeWindowAssociation(RenderForm.Handle, WindowAssociationFlags.IgnoreAll); var backBuffer = FromSwapChain <Texture2D>(SwapChain, 0); RenderTargetView = new RenderTargetView(Device, backBuffer); RenderTarget2D = new RenderTarget(D2dFactory, backBuffer.QueryInterface <Surface>(), new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); DepthStencilView = new DepthStencilView(Device, new Texture2D(Device, new Texture2DDescription { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = RenderForm.ClientSize.Width, Height = RenderForm.ClientSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None })); DirectWrite = new SharpDX.DirectWrite.Factory(); Device.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, RenderForm.ClientSize.Width, RenderForm.ClientSize.Height, 0.0f, 1.0f)); }
/// <summary> /// /// </summary> /// <param name="parameters"></param> public GenericDisplay(Game game, GraphicsDevice device, GameParameters parameters) : base(game, device, parameters) { window = CreateTouchForm(parameters, null); try { NvApi.Initialize(); NvApi.Stereo_Disable(); } catch (NVException nvex) { Log.Debug(nvex.Message); } //var deviceFlags = DeviceCreationFlags.SingleThreaded; var deviceFlags = DeviceCreationFlags.None; deviceFlags |= parameters.UseDebugDevice ? DeviceCreationFlags.Debug : DeviceCreationFlags.None; deviceFlags |= parameters.SupportVideo ? DeviceCreationFlags.BgraSupport | (DeviceCreationFlags)2048 : DeviceCreationFlags.None; var driverType = DriverType.Hardware; var featureLevel = HardwareProfileChecker.GetFeatureLevel(parameters.GraphicsProfile); swapChainDesc = new SwapChainDescription() { BufferCount = 2, ModeDescription = new ModeDescription(parameters.Width, parameters.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = window.Handle, SampleDescription = new SampleDescription(parameters.MsaaLevel, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, Flags = SwapChainFlags.None, }; D3D.Device.CreateWithSwapChain(driverType, deviceFlags, new[] { featureLevel }, swapChainDesc, out d3dDevice, out swapChain); //Log.Message(" compute shaders : {0}", d3dDevice.CheckFeatureSupport(Feature.ComputeShaders) ); //Log.Message(" shader doubles : {0}", d3dDevice.CheckFeatureSupport(Feature.ShaderDoubles) ); //Log.Message(" threading : {0}", d3dDevice.CheckFeatureSupport(Feature.Threading) ); bool driverConcurrentCreates; bool driverCommandLists; d3dDevice.CheckThreadingSupport(out driverConcurrentCreates, out driverCommandLists); d3dDevice.GetCounterCapabilities(); Log.Message(" Concurrent Creates : {0}", driverConcurrentCreates); Log.Message(" Command Lists : {0}", driverCommandLists); var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(window.Handle, WindowAssociationFlags.IgnoreAll); clientWidth = window.ClientSize.Width; clientHeight = window.ClientSize.Height; }
/// <summary> /// Initialise the device and set up a render window /// </summary> public static RenderForm InitD3D( out SlimDX.Direct3D11.Device device, out SwapChain swapChain, out RenderTargetView renderView) { var form = new RenderForm("Solar simulation"); form.Width = 1080; form.Height = 1080; var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); Factory factory = swapChain.GetParent <Factory>(); factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); RasterizerStateDescription rsd = new RasterizerStateDescription() { CullMode = CullMode.None, DepthBias = 0, DepthBiasClamp = 0.0f, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsDepthClipEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = false, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }; RasterizerState rs = RasterizerState.FromDescription(device, rsd); device.ImmediateContext.Rasterizer.State = rs; Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); device.ImmediateContext.OutputMerger.SetTargets(renderView); device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f)); rs.Dispose(); backBuffer.Dispose(); return(form); }
public void InitDirectX() { if (IsDirectXInitialized) { throw new InvalidOperationException(); } form = new RenderForm("The Wizards - DirectX 11"); form.Activated += new EventHandler(form_Activated); form.Deactivate += new EventHandler(form_Deactivate); form.Size = FormSize; //form.Width = (int)FormSize.X; //form.Height = (int)FormSize.Y; //form.TopMost = true; //WARNING: THERE IS ONLY ONE BUFFER ATM. (no backbuffer?) var desc = new SwapChainDescription { BufferCount = 3, // Set to 3 here for triple buffering? //ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(0, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), // No multisampling! //SampleDescription = ne w SampleDescription(4, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.BackBuffer, // backbuffer added, no clue wat it does }; try { //Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain); Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); } catch (Exception) { Console.WriteLine("Unable to initialize device in Debug Mode. Running in normal mode instead!"); Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain); } //var result = device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 2); var factory = swapChain.GetParent <Factory>(); factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderTargetView = new RenderTargetView(device, BackBuffer); device.ImmediateContext.OutputMerger.SetTargets(renderTargetView); device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f)); }
internal Renderer(LoopWindow Window) { var Levels = new[] { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3, FeatureLevel.Level_9_2, FeatureLevel.Level_9_1, }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport | (Program.DebugMode ? DeviceCreationFlags.Debug : DeviceCreationFlags.None), Levels, new SwapChainDescription { BufferCount = 1, Flags = SwapChainFlags.AllowModeSwitch, IsWindowed = true, ModeDescription = new ModeDescription { Format = FormatRGB }, OutputHandle = Window.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }, out Device, out SwapChain); var Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; using (var Factory = SwapChain.GetParent <Factory>()) { foreach (var Output in Factory.Adapters.First().Outputs) { foreach (var PossibleResolution in Output.GetDisplayModeList(FormatRGB, 0)) { if (PossibleResolution.Width == Bounds.Width && PossibleResolution.Height == Bounds.Height) { Resolution = PossibleResolution; } } } } Window.Borderless(Resolution.Width, Resolution.Height); ResizeBuffers(); Depth.InitStencilRasterState(Device); LoadWithAA(1); Device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; if (Program.DebugMode) { Debug = new DeviceDebug(Device); } }
private void construct(RenderControl form, Format format, bool debug) { View = form; IntPtr hndl = GetHandle(View); SD.Size sz = GetSize(View); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1,//buffer count ModeDescription = new ModeDescription() { Width = sz.Width, Height = sz.Height, /*new Rational(60, 1),*/ Format = format }, //sview IsWindowed = true, OutputHandle = hndl, //View.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; FeatureLevel[] levels = new FeatureLevel[] { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 }; //create device and swapchain DeviceCreationFlags flag = DeviceCreationFlags.BgraSupport; if (debug) { flag |= DeviceCreationFlags.Debug; } Device11.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, flag, levels, desc, out _device, out _swapchain); //get context to device _deviceContext = Device.ImmediateContext; //Ignore all windows events var factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(hndl /*View.Handle*/, WindowAssociationFlags.IgnoreAll); //Setup handler on resize form View.Resize += (sender, args) => MustResize = true; //Set Default State SetDefaultRasterState(); SetDefaultDepthState(); SetDefaultBlendState(); SetDefaultSamplerState(); //Resize all items Resize(); }
public MainWindow() : base("Heat Simulator") { ClientSize = new Size(400, 400); MouseClick += MainWindow_MouseClick; MouseMove += MainWindow_MouseMove; MaximizeBox = false; KeyPreview = true; KeyPress += MainWindow_KeyPress; var desc = new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(Width, Height, new Rational(60, 1), Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { FeatureLevel.Level_12_0, FeatureLevel.Level_11_0 }, desc, out device, out swapChain); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); factory2d = new Factory2D(); renderTarget2d = new RenderTarget(factory2d, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); sColorBrush = new SolidColorBrush(renderTarget2d, Color4.White); factoryWrite = new FactoryW(); textFormat = new TextFormat(factoryWrite, "Ricty", 25) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near }; renderTarget2d.TextAntialiasMode = TextAntialiasMode.Cleartype; textArea = new RectangleF(0, 0, Width / 2f, Height / 2f); simulator = new HeatSimulator(); stopWatch = new Stopwatch(); cells = new GridCell[simulator.DivNum, simulator.DivNum]; for (var i = 0; i < simulator.DivNum; i++) { for (var j = 0; j < simulator.DivNum; j++) { cells[i, j] = new GridCell(factory2d, Width, Height, i, j, simulator.DivNum, simulator.State); } } }
public Game(string windowName, int windowWidth, int windowHeight, bool isWindowed = true) { RenderForm = new RenderForm(windowName); RenderForm.Width = windowWidth; RenderForm.Height = windowHeight; RenderForm.AllowUserResizing = false; var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription( (int)(RenderForm.Width), (int)(RenderForm.Height), new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = isWindowed, OutputHandle = RenderForm.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out Device, out SwapChain); Factory2D = new SharpDX.Direct2D1.Factory(); Factory factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(RenderForm.Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(SwapChain, 0); Surface = backBuffer.QueryInterface <Surface>(); RenderTarget2D = new RenderTarget(Factory2D, Surface, new RenderTargetProperties( new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); //WEB_SOCKET Connect(); System.Threading.Tasks.Task.Delay(1000); _gameRender = new GameRender(RenderForm, Factory2D, RenderTarget2D); _directInput = new DirectInput(); _keyboard = new Keyboard(_directInput); _keyboard.Properties.BufferSize = 128; _keyboard.Acquire(); }
public RForm(string text) : base(text) { this.ClientSize = new System.Drawing.Size(screenWidth, screenHeight); desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(this.ClientSize.Width, this.ClientSize.Height, new Rational(144, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = this.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); d2dFactory = new SharpDX.Direct2D1.Factory(); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(this.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); solidColorBrush.Color = Color.White; directInput = new DirectInput(); keyboard = new Keyboard(directInput); keyboard.Properties.BufferSize = 128; keyboard.Acquire(); userInputProcessor = new UserInputProcessor(); scoreTextFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); scoreTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(10, 10, 400, 400); livesTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(10, 46, 400, 400); gameOverTextFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 108); gameOverTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(0, 0, screenWidth, screenHeight); gameOverTextFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center; gameOverTextFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;; rand = new Random(); ship = new Ship(screenWidth / 2, screenHeight / 2); bullets = new List <Bullet>(); asteroids = new List <Asteroid>(); int count = rand.Next(10, 15); for (int i = 0; i < count; i++) { asteroids.Add(new Asteroid(rand.Next(-25, screenWidth + 25), rand.Next(-25, screenWidth + 25), rand.Next(1, 4) * 10, rand, d2dFactory)); } gameInputTimer = new Stopwatch(); gameInputTimer.Start(); }
/// <summary> /// Initialise les ressources graphiques de base. /// </summary> void InitializeGraphics() { var form = new RenderForm("Test SlimDX"); form.ClientSize = new System.Drawing.Size(Scene.Instance.ResolutionWidth, Scene.Instance.ResolutionHeight); m_renderForm = form; var description = new SwapChainDescription() { BufferCount = 2, Usage = Usage.RenderTargetOutput, OutputHandle = form.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(2, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out m_device, out m_swapChain); // Depth stencil buffer var depthStencilDesc = new Texture2DDescription() { Width = Scene.Instance.ResolutionWidth, Height = Scene.Instance.ResolutionHeight, MipLevels = 1, ArraySize = 1, Format = Format.D32_Float_S8X24_UInt, SampleDescription = new SampleDescription(2, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; m_depthStencilBuffer = new Texture2D(m_device, depthStencilDesc); m_depthStencilView = new DepthStencilView(m_device, m_depthStencilBuffer); // Création d'une vue sur le render target. using (var resource = Resource.FromSwapChain <Texture2D>(m_swapChain, 0)) m_mainRenderTarget = new RenderTargetView(m_device, resource); // Création du viewport. var context = m_device.ImmediateContext; var viewport = new Viewport(0.0f, 0.0f, Scene.Instance.ResolutionWidth, Scene.Instance.ResolutionHeight); context.OutputMerger.SetTargets(m_depthStencilView, m_mainRenderTarget); context.Rasterizer.SetViewports(viewport); // Empêche le alt + enter de fonctionner. using (var factory = m_swapChain.GetParent <Factory>()) factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter); }
public void Initialise(RenderForm renderForm, bool windowed) { ModeDescription modeDescription = DescribeBuffer(renderForm.ClientSize.Width, renderForm.ClientSize.Height); swapChainDescription = DescribeSwapChain(modeDescription, renderForm, windowed); CreateDevice(swapChainDescription); // Ignore all windows events Factory factory = _swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll); AssignDeviceContext(); CreateRenderTargetView(); }
private void InitRenderer() { swapChainDescription = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDescription, out device, out swapChain); context = device.ImmediateContext; var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); factory.Dispose(); form.UserResized += (sender, args) => { var sform = (RenderForm)sender; RenderContext.ViewportDirty = true; RenderContext.ViewportSize = new Vector2(sform.ClientSize.Width, sform.ClientSize.Height); }; form.MouseLeave += (sender, args) => RenderContext.HasMouseFocus = false; form.MouseEnter += (sender, args) => RenderContext.HasMouseFocus = true; form.GotFocus += (sender, args) => RenderContext.HasMouseFocus = true; form.LostFocus += (sender, args) => RenderContext.HasMouseFocus = false; form.MouseMove += (sender, args) => { /* * RenderContext.LastMousePosition = new Vector2( args.X, args.Y ); * RenderContext.LastMouseWorldPosition = new Vector2( args.X, args.Y ); * RenderContext.LastMouseWorldPosition -= RenderContext.ViewportSize / 2.0f; * var mouseWorldPosTransformed = new Vector4( RenderContext.LastMouseWorldPosition.X, RenderContext.LastMouseWorldPosition.Y, 0, 1 ); * RenderContext.LastMouseWorldPosition = new Vector2(mouseWorldPosTransformed.X, mouseWorldPosTransformed.Y); */ }; RenderContext = new DocumentRenderContext(); RenderContext.ViewportSize = new Vector2(form.ClientSize.Width, form.ClientSize.Height); RenderContext.ViewportDirty = true; immediateContext = new ImmediateContext(); immediateContext.Init(device); renderLoop = new RenderLoop(form); }
/// <summary> /// Init all object to start rendering /// </summary> /// <param name="form">Rendering form</param> /// <param name="debug">Active the debug mode</param> public SharpDevice(RenderForm form, bool debug = false) { View = form; // SwapChain description var desc = new SwapChainDescription() { BufferCount = 2, //buffer count ModeDescription = new ModeDescription(View.ClientSize.Width, View.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), //sview IsWindowed = true, OutputHandle = View.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // 入っているDirectXが10までならここを 10_1に FeatureLevel[] levels = new FeatureLevel[] { FeatureLevel.Level_11_1 }; //create device and swapchain DeviceCreationFlags flag = DeviceCreationFlags.None | DeviceCreationFlags.BgraSupport; if (debug) { flag = DeviceCreationFlags.Debug; } Device11.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, flag, levels, desc, out _device, out _swapchain); //get context to device _deviceContext = Device.ImmediateContext; //Ignore all windows events var factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(View.Handle, WindowAssociationFlags.IgnoreAll); //Setup handler on resize form View.UserResized += (sender, args) => MustResize = true; //Set Default State SetDefaultRasterState(); SetDefaultDepthState(); SetDefaultBlendState(); SetDefaultSamplerState(); Font = new Sharp2D(this); //Resize all items Resize(); }
/// <summary> /// Init all object to start rendering /// </summary> /// <param name="form">Rendering form</param> /// <param name="debug">Active the debug mode</param> public SharpDevice(System.Windows.Forms.Control control, bool debug = false) { Control = control; // SwapChain description SwapChainDescription desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(Control.ClientSize.Width, Control.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = Control.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; //if (GetSupportedFeatureLevel() >= FeatureLevel.Level_10_0) // featureLevel = GetSupportedFeatureLevel(); FeatureLevel[] levels = new FeatureLevel[] { featureLevel }; //create device and swapchain DeviceCreationFlags flag = DeviceCreationFlags.None; if (debug) { flag = DeviceCreationFlags.Debug; } Device11.CreateWithSwapChain(DriverType.Hardware, flag, levels, desc, out _device, out _swapchain); //get context to device _deviceContext = Device.ImmediateContext; //Ignore all windows events var factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Control.Handle, WindowAssociationFlags.IgnoreAll); //Setup handler on resize form Control.Resize += (sender, args) => MustResize = true; //Set Default State ApplyRasterState(); SetDefaultDepthState(); SetDefaultBlendState(); //Resize all items Resize(); }
private static MyRenderDeviceSettings CreateDeviceInternal(IntPtr windowHandle, MyRenderDeviceSettings? settingsToTry) { if (Device != null) { Device.Dispose(); Device = null; } if (settingsToTry != null) { Log.WriteLine("CreateDevice - original settings"); var originalSettings = settingsToTry.Value; LogSettings(ref originalSettings); } FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 }; DeviceCreationFlags flags = DeviceCreationFlags.None; #if DEBUG_DEVICE && DEBUG flags |= DeviceCreationFlags.Debug; #endif WinApi.DEVMODE mode = new WinApi.DEVMODE(); WinApi.EnumDisplaySettings(null, WinApi.ENUM_REGISTRY_SETTINGS, ref mode); var settings = settingsToTry ?? new MyRenderDeviceSettings() { AdapterOrdinal = -1, BackBufferHeight = mode.dmPelsHeight, BackBufferWidth = mode.dmPelsWidth, WindowMode = MyWindowModeEnum.Fullscreen, RefreshRate = 60000, VSync = false, }; settings.AdapterOrdinal = ValidateAdapterIndex(settings.AdapterOrdinal); if (settings.AdapterOrdinal == -1) { throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported); } m_settings = settings; Log.WriteLine("CreateDevice settings"); LogSettings(ref m_settings); // If this line crashes cmd this: Dism /online /add-capability /capabilityname:Tools.Graphics.DirectX~~~~0.0.1.0 var adapters = GetAdaptersList(); if (m_settings.AdapterOrdinal >= adapters.Length) throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported); var adapterId = adapters[m_settings.AdapterOrdinal].AdapterDeviceId; if (adapterId >= GetFactory().Adapters.Length) throw new MyRenderException("Invalid adapter id binding!", MyRenderExceptionEnum.GpuNotSupported); var adapter = GetFactory().Adapters[adapterId]; Device = new Device(adapter, flags, FeatureLevel.Level_11_0); // HACK: This is required for Steam overlay to work. Apparently they hook only CreateDevice methods with DriverType argument. try { using (new Device(DriverType.Hardware, flags, FeatureLevel.Level_11_0)){} } catch { } if (flags.HasFlag(DeviceCreationFlags.Debug)) { if (DebugDevice != null) { DebugDevice.Dispose(); DebugDevice = null; } DebugDevice = new DeviceDebug(Device); DebugInfoQueue = DebugDevice.QueryInterface<InfoQueue>(); new System.Threading.Thread(ProcessDebugOutput).Start(); } if(DeviceContext != null) { DeviceContext.Dispose(); DeviceContext = null; } DeviceContext = Device.ImmediateContext; m_windowHandle = windowHandle; m_resolution = new Vector2I(m_settings.BackBufferWidth, m_settings.BackBufferHeight); if (!m_initialized) { InitSubsystems(); m_initialized = true; } if (m_swapchain != null) { m_swapchain.Dispose(); m_swapchain = null; } if (m_swapchain == null) { SharpDX.DXGI.Device d = Device.QueryInterface<SharpDX.DXGI.Device>(); Adapter a = d.GetParent<Adapter>(); var factory = a.GetParent<Factory>(); var scDesc = new SwapChainDescription(); scDesc.BufferCount = MyRender11Constants.BUFFER_COUNT; scDesc.Flags = SwapChainFlags.AllowModeSwitch; scDesc.IsWindowed = true; scDesc.ModeDescription.Format = MyRender11Constants.DX11_BACKBUFFER_FORMAT; scDesc.ModeDescription.Height = m_settings.BackBufferHeight; scDesc.ModeDescription.Width = m_settings.BackBufferWidth; scDesc.ModeDescription.RefreshRate.Numerator = m_settings.RefreshRate; scDesc.ModeDescription.RefreshRate.Denominator = 1000; scDesc.ModeDescription.Scaling = DisplayModeScaling.Unspecified; scDesc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Progressive; scDesc.SampleDescription.Count = 1; scDesc.SampleDescription.Quality = 0; scDesc.OutputHandle = m_windowHandle; scDesc.Usage = Usage.RenderTargetOutput; scDesc.SwapEffect = SwapEffect.Discard; m_swapchain = new SwapChain(factory, Device, scDesc); m_swapchain.GetParent<Factory>().MakeWindowAssociation(m_windowHandle, WindowAssociationFlags.IgnoreAll); } // we start with window always (DXGI recommended) m_settings.WindowMode = MyWindowModeEnum.Window; ApplySettings(settings); return m_settings; }
internal static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings? settingsToTry) { if (Device != null) { Device.Dispose(); Device = null; } FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 }; DeviceCreationFlags flags = DeviceCreationFlags.None; #if DEBUG_DEVICE flags |= DeviceCreationFlags.Debug; #endif WinApi.DEVMODE mode = new WinApi.DEVMODE(); WinApi.EnumDisplaySettings(null, WinApi.ENUM_REGISTRY_SETTINGS, ref mode); var adapters = GetAdaptersList(); int adapterIndex = settingsToTry.HasValue ? settingsToTry.Value.AdapterOrdinal : - 1; bool adapterIndexNotValid = adapterIndex == -1 || adapters.Length <= settingsToTry.Value.AdapterOrdinal || !adapters[settingsToTry.Value.AdapterOrdinal].IsSupported; if(adapterIndexNotValid) { var maxVram = 0ul; for(int i=0; i<adapters.Length; i++) { if(adapters[i].IsSupported) { maxVram = (ulong) Math.Max(maxVram, adapters[i].VRAM); } } // taking supporting adapter with most VRAM for (int i = 0; i < adapters.Length; i++) { if(adapters[i].IsSupported && adapters[i].VRAM == maxVram) { adapterIndex = i; break; } } } if(adapterIndex == -1) { throw new MyRenderException("No supporting device detected!", MyRenderExceptionEnum.GpuNotSupported); } var settings = settingsToTry ?? new MyRenderDeviceSettings() { AdapterOrdinal = adapterIndex, BackBufferHeight = mode.dmPelsHeight, BackBufferWidth = mode.dmPelsWidth, WindowMode = MyWindowModeEnum.Fullscreen, VSync = false, }; m_settings = settings; Device = new Device(GetFactory().Adapters[adapters[m_settings.AdapterOrdinal].AdapterDeviceId], flags, FeatureLevel.Level_11_0); // HACK: This is required for Steam overlay to work. Apparently they hook only CreateDevice methods with DriverType argument. try { using (new Device(DriverType.Hardware, flags, FeatureLevel.Level_11_0)){} } catch { } if (flags.HasFlag(DeviceCreationFlags.Debug)) { if (DebugDevice != null) { DebugDevice.Dispose(); DebugDevice = null; } DebugDevice = new DeviceDebug(Device); DebugInfoQueue = DebugDevice.QueryInterface<InfoQueue>(); new System.Threading.Thread(ProcessDebugOutput).Start(); } if(ImmediateContext != null) { ImmediateContext.Dispose(); ImmediateContext = null; } ImmediateContext = Device.ImmediateContext; m_windowHandle = windowHandle; m_resolution = new Vector2I(m_settings.BackBufferWidth, m_settings.BackBufferHeight); if (!m_initialized) { InitSubsystems(); m_initialized = true; } if (m_swapchain != null) { m_swapchain.Dispose(); m_swapchain = null; } if (m_swapchain == null) { SharpDX.DXGI.Device d = Device.QueryInterface<SharpDX.DXGI.Device>(); Adapter a = d.GetParent<Adapter>(); var factory = a.GetParent<Factory>(); var scDesc = new SwapChainDescription(); scDesc.BufferCount = MyRender11Constants.BUFFER_COUNT; scDesc.Flags = SwapChainFlags.AllowModeSwitch; scDesc.IsWindowed = true; scDesc.ModeDescription.Format = MyRender11Constants.BACKBUFFER_FORMAT; scDesc.ModeDescription.Height = m_settings.BackBufferHeight; scDesc.ModeDescription.Width = m_settings.BackBufferWidth; scDesc.ModeDescription.RefreshRate.Numerator = m_settings.RefreshRate; scDesc.ModeDescription.RefreshRate.Denominator = 1000; scDesc.ModeDescription.Scaling = DisplayModeScaling.Unspecified; scDesc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Progressive; scDesc.SampleDescription.Count = 1; scDesc.SampleDescription.Quality = 0; scDesc.OutputHandle = m_windowHandle; scDesc.Usage = Usage.RenderTargetOutput; scDesc.SwapEffect = SwapEffect.Discard; m_swapchain = new SwapChain(factory, Device, scDesc); m_swapchain.GetParent<Factory>().MakeWindowAssociation(m_windowHandle, WindowAssociationFlags.IgnoreAll); } // we start with window always (DXGI recommended) m_settings.WindowMode = MyWindowModeEnum.Window; ApplySettings(settings); return m_settings; }
public void CreateDeviceAndSwapChain(RenderForm form) { FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0, FeatureLevel.Level_10_0 }; Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels); if(ConfigurationManager.Config.AntiAliasing) _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4); var swapChainDescription = new SwapChainDescription { BufferCount = 1, // Set to a single back buffer (double buffering) ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm), IsWindowed = ConfigurationManager.Config.WindowedMode, OutputHandle = form.Handle, SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel-1) : new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SwapChain = new SwapChain(new Factory(), Device, swapChainDescription); DeviceContext = Device.ImmediateContext; SwapChain.GetParent<Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); }
public void CreateDeviceAndSwapChain(RenderForm form) { FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 }; #if GPU_DEBUG //Use https://dev.windows.com/en-us/develop/graphics-debugging-and-frame-analysis-update on Windows 10 to install debug layer. Device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug, featureLevels); #else Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels); #endif if(ConfigurationManager.Config.AntiAliasing) _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4); var swapChainDescription = new SwapChainDescription { BufferCount = 1, // Set to a single back buffer (double buffering) ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm), IsWindowed = ConfigurationManager.Config.WindowedMode, OutputHandle = form.Handle, SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel-1) : new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SwapChain = new SwapChain(new Factory(), Device, swapChainDescription); DeviceContext = Device.ImmediateContext; SwapChain.GetParent<Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); }