/// <summary> /// Creates a default D3D11 Texture with forced Shared-Flag /// </summary> /// <param name="device"></param> /// <param name="description"></param> /// <param name="D3D10Dev"> </param> /// <param name="D2DFactory"> </param> public SharedTexture(D2DInteropHandler handler, Texture2DDescription description) { As11Tex = new Texture2D(handler.D3DDevice11, new Texture2DDescription() { ArraySize = description.ArraySize, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, CpuAccessFlags = description.CpuAccessFlags, Format = description.Format, Height = description.Height, MipLevels = description.MipLevels, OptionFlags = ResourceOptionFlags.KeyedMutex, SampleDescription = description.SampleDescription, Usage = description.Usage, Width = description.Width }); Mutex11 = new KeyedMutex(As11Tex); AsResource = new SlimDX.DXGI.Resource(As11Tex); As10Tex = handler.D3DDevice10.OpenSharedResource<SlimDX.Direct3D10.Texture2D>(AsResource.SharedHandle); Mutex10 = new KeyedMutex(As10Tex); AsSurface = As10Tex.AsSurface(); As2DTarget = SlimDX.Direct2D.RenderTarget.FromDXGI(handler.D2DFactory, AsSurface, new RenderTargetProperties() { MinimumFeatureLevel = FeatureLevel.Direct3D10, Usage = RenderTargetUsage.None, Type = RenderTargetType.Hardware, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied) }); }
/// <summary> /// Initializes the graphics. /// </summary> public void Initialize() { var descMode = new ModeDescription { Width = _graphicsDevice.BackBuffer.Width, Height = _graphicsDevice.BackBuffer.Height, Format = Format.R8G8B8A8_UNorm, RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Stretched }; var descSwap = new SwapChainDescription { BufferCount = 1, ModeDescription = descMode, Usage = Usage.RenderTargetOutput, OutputHandle = _graphicsDevice.RenderTarget.Handle, SampleDescription = new SampleDescription(1, 0), IsWindowed = true, SwapEffect = SwapEffect.Discard, Flags = SwapChainFlags.None, }; Result creationResult = Device1.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0, descSwap, out _direct3D10Device, out _swapChain); if (creationResult.IsFailure) { throw new GraphicsException("DirectX10 is not supported on the current platform."); } SlimDX.DXGI.Surface backBuffer = SlimDX.DXGI.Surface.FromSwapChain(_swapChain, 0); var d2DFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded); RenderTarget renderTarget = RenderTarget.FromDXGI(d2DFactory, backBuffer, new RenderTargetProperties { HorizontalDpi = 96, VerticalDpi = 96, MinimumFeatureLevel = SlimDX.Direct2D.FeatureLevel.Default, PixelFormat = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), Type = RenderTargetType.Hardware, Usage = RenderTargetUsage.None }); renderTarget.AntialiasMode = SmoothingMode == SmoothingMode.AntiAlias ? AntialiasMode.Aliased : AntialiasMode.PerPrimitive; renderTarget.TextAntialiasMode = TextAntialiasMode.ClearType; _renderTarget = renderTarget; DirectXHelper.RenderTarget = _renderTarget; DirectXHelper.Direct2DFactory = d2DFactory; }
public void ResizeDevice(int width, int height) { lock (this) { if (width < 0) { throw new ArgumentOutOfRangeException("width", "Value must be positive."); } if (height < 0) { throw new ArgumentOutOfRangeException("height", "Value must be positive."); } if ((width <= this.width) && (height <= this.height)) { return; } // Recreate the render target // Assign result to temporary variable in case CreateTexture2D throws an exception. var texture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), true); if (this.texture != null) { this.texture.Dispose(); } this.texture = texture; var shareableTexture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), false); if (this.shareableTexture != null) { this.shareableTexture.Dispose(); } this.shareableTexture = shareableTexture; //if (renderTargetView != null) renderTargetView.Dispose(); //this.renderTargetView = new RenderTargetView(device, shareableTexture); this.width = texture.Description.Width; this.height = texture.Description.Height; SlimDX.DXGI.Surface surface = texture.AsSurface(); CreateRenderTarget(surface); if (DeviceResized != null) { DeviceResized(this, EventArgs.Empty); } } }
private void CreateRenderTarget(SlimDX.DXGI.Surface surface) { // Create a D2D render target which can draw into our offscreen D3D // surface. D2D uses device independant units, like WPF, at 96/inch var properties = new SlimDX.Direct2D.RenderTargetProperties(); properties.HorizontalDpi = 96; properties.VerticalDpi = 96; properties.MinimumFeatureLevel = SlimDX.Direct2D.FeatureLevel.Default; //properties.PixelFormat = new SlimDX.Direct2D.PixelFormat(Format.Unknown, SlimDX.Direct2D.AlphaMode.Premultiplied); properties.PixelFormat = new SlimDX.Direct2D.PixelFormat(Format.Unknown, SlimDX.Direct2D.AlphaMode.Premultiplied); //properties.RenderTargetType = D2D.RenderTargetType.Default; properties.Usage = SlimDX.Direct2D.RenderTargetUsage.None; if (this.renderTarget != null) { this.renderTarget.Dispose(); } renderTarget = SlimDX.Direct2D.RenderTarget.FromDXGI(factory2D, surface, properties); }
public void Init() { #region init_gfx Form = new RenderForm("Kazedan"); var factory = new FactoryD2D(); SizeF dpi = factory.DesktopDpi; var swapChainDesc = new SwapChainDescription() { BufferCount = 2, Usage = Usage.RenderTargetOutput, OutputHandle = Form.Handle, IsWindowed = true, ModeDescription = new ModeDescription((int)(GFXResources.Bounds.Width * (dpi.Width / 96f)), (int)(GFXResources.Bounds.Height * (dpi.Height / 96f)), new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard, }; Device device; SwapChain swapChain; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, swapChainDesc, out device, out swapChain); Surface backBuffer = Surface.FromSwapChain(swapChain, 0); renderTarget = RenderTarget.FromDXGI(factory, backBuffer, new RenderTargetProperties() { HorizontalDpi = dpi.Width, VerticalDpi = dpi.Height, MinimumFeatureLevel = SlimDX.Direct2D.FeatureLevel.Default, PixelFormat = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Ignore), Type = RenderTargetType.Default, Usage = RenderTargetUsage.None }); factory.Dispose(); // Freaking antialiasing lagging up my programs renderTarget.AntialiasMode = AntialiasMode.Aliased; renderTarget.TextAntialiasMode = TextAntialiasMode.Grayscale; using (var DXGIFactory = swapChain.GetParent <FactoryDXGI>()) DXGIFactory.SetWindowAssociation(Form.Handle, WindowAssociationFlags.IgnoreAltEnter); Form.ClientSize = GFXResources.Bounds; Form.AutoSizeMode = AutoSizeMode.GrowAndShrink; Form.Icon = Properties.Resources.KazedanIcon; #endregion GFXResources.Init(renderTarget); Form.KeyDown += (o, e) => { Keys key = e.KeyCode; switch (key) { case Keys.F11: swapChain.IsFullScreen = !swapChain.IsFullScreen; break; case Keys.F: Sequencer.NoteManager.UserEnabledFancy = !Sequencer.NoteManager.UserEnabledFancy; Sequencer.NoteManager.RenderFancy = Sequencer.NoteManager.UserEnabledFancy; break; case Keys.Up: Sequencer.Delay += 100; Sequencer.Reset(); break; case Keys.Down: if (Sequencer.Delay >= 100) { Sequencer.Delay -= 100; Sequencer.Reset(); } break; case Keys.Left: if (GFXResources.NoteOffset > 0) { GFXResources.NoteOffset--; } break; case Keys.Right: if (GFXResources.NoteOffset < 128 - GFXResources.NoteCount) { GFXResources.NoteOffset++; } break; case Keys.D: Sequencer.ShowDebug = !Sequencer.ShowDebug; break; case Keys.Space: if (Sequencer.Stopped) { Sequencer.Start(); } else { Sequencer.Stop(); } break; } }; // Start loading thread Thread loadThread = new Thread(Load); loadThread.Start(); // Show controller window Thread controlThread = new Thread(() => { loadThread.Join(); Application.EnableVisualStyles(); ControlForm = new KZControl(Sequencer); Application.Run(ControlForm); }); controlThread.SetApartmentState(ApartmentState.STA); controlThread.Start(); // Make sure the control form closes when the main form does Form.Disposed += (o, e) => { ControlForm?.BeginInvoke((MethodInvoker) delegate { ControlForm.Close(); }); }; MessagePump.Run(Form, () => { // Do sequencer tick if (!Sequencer.Stopped) { Sequencer.UpdateNotePositions(); Sequencer.UpdateRenderer(); } Paint(renderTarget); // Calculate profiling information long tick = Environment.TickCount; if (tick - LastSample >= SampleRate) { Elapsed = tick - LastTick; LastSample = tick; } LastTick = tick; swapChain.Present(1, PresentFlags.None); }); renderTarget.Dispose(); swapChain.Dispose(); device.Dispose(); Sequencer.Dispose(); }