protected virtual DepthStencilState CreateDepthStencilState(global::SharpDX.Direct3D11.Device device) { return(new DepthStencilState(device, new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.LessEqual })); }
/// <summary> /// Attaches the element to the specified host. /// </summary> /// <param name="host">The host.</param> public virtual void Attach(IRenderHost host) { this.isAttached = true; this.device = host.Device; this.effect = host.Effects[effectName]; this.renderHost = host; }
/// <summary> /// /// </summary> /// <param name="host"></param> /// <param name="format"></param> internal void CreateBuffers(IRenderHost host, Format format = Format.R32G32B32A32_Float) { /// set class variables this.renderHost = host; this.device = host.Device; this.targetWidth = Math.Max((int)host.ActualWidth, 100); this.targetHeight = Math.Max((int)host.ActualHeight, 100); /// clear old buffers this.ClearGBuffer(); this.screenQuad.Dispose(); this.screenSphere.Dispose(); this.screenCone.Dispose(); /// create new buffers this.CreateQuadBuffer(); this.CreateSphereBuffer(); this.CreateConeBuffer(); this.CreateGBuffer(format); /// flush this.device.ImmediateContext.Flush(); }
protected override DepthStencilState CreateDepthStencilState(global::SharpDX.Direct3D11.Device device) { return(new DepthStencilState(device, new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false })); }
/// <summary> /// Creates device resources. /// </summary> /// <remarks> /// This method is called at the initialization of this instance. /// </remarks> protected virtual void CreateDeviceResources() { // Dispose previous references and set to null if (d3dDevice != null) { RemoveAndDispose(ref d3dDevice); } if (d3dContext != null) { RemoveAndDispose(ref d3dContext); } if (d2dDevice != null) { RemoveAndDispose(ref d2dDevice); } if (d2dContext != null) { RemoveAndDispose(ref d2dContext); } // Allocate new references // Enable compatibility with Direct2D // Retrieve the Direct3D 11.1 device amd device context var creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; // Decomment this line to have Debug. Unfortunately, debug is sometimes crashing applications, so it is disable by default try { // Try to create it with Video Support // If it is not working, we just use BGRA // Force to FeatureLevel.Level_9_1 using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags)) d3dDevice = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>(); } catch (Exception) { creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags)) d3dDevice = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>(); } featureLevel = d3dDevice.FeatureLevel; // Get Direct3D 11.1 context d3dContext = Collect(d3dDevice.ImmediateContext.QueryInterface <global::SharpDX.Direct3D11.DeviceContext1>()); // Create Direct2D device using (var dxgiDevice = d3dDevice.QueryInterface <global::SharpDX.DXGI.Device>()) d2dDevice = Collect(new global::SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice)); // Create Direct2D context d2dContext = Collect(new global::SharpDX.Direct2D1.DeviceContext(d2dDevice, global::SharpDX.Direct2D1.DeviceContextOptions.None)); }
protected override DeviceDescriptor CreateDevicesAndFactories() { DeviceDescriptor desc = new DeviceDescriptor(); var creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; var featureLevels = global::SharpDX.Direct3D.FeatureLevel.Level_10_0; using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags, featureLevels)) desc.D3DDevice10 = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>(); desc.WriteFactory = new global::SharpDX.DirectWrite.Factory(global::SharpDX.DirectWrite.FactoryType.Shared); desc.D2DFactory = new global::SharpDX.Direct2D1.Factory(global::SharpDX.Direct2D1.FactoryType.SingleThreaded); return(desc); }
public DeviceManager(Device device) { d3dDevice = device; d3dContext = device.ImmediateContext; }
public DuplicationResource(global::SharpDX.Direct3D11.Device device) { this.device = device; }
protected override IDisposable CreateFontMapTexture(int width, int height, CharRenderCall[] drawCalls) { var TexDesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var device10 = (global::SharpDX.Direct3D11.Device)D3DDevice10; var texture = new Texture2D(device10, TexDesc); var rtv = new RenderTargetView(device10, texture); device10.ImmediateContext.ClearRenderTargetView(rtv, new Color4(1, 1, 1, 0)); Surface surface = texture.QueryInterface <Surface>(); var target = new RenderTarget((global::SharpDX.Direct2D1.Factory)D2DFactory, surface, rtp); var color = new SolidColorBrush(target, new Color4(1, 1, 1, 1)); target.BeginDraw(); foreach (var drawCall in drawCalls) { target.DrawTextLayout(new Vector2(drawCall.Position.X, drawCall.Position.Y), (TextLayout)drawCall.TextLayout, color); } target.EndDraw(); color.Dispose(); //This is a workaround for Windows 8.1 machines. //If these lines would not be present, the shared resource would be empty. //TODO: find a nicer solution #region WorkAround var textureDescDummy = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Staging }; global::SharpDX.Direct3D11.Device device = (global::SharpDX.Direct3D11.Device)D3DDevice10; var textureDummy = new Texture2D(device, textureDescDummy); DataStream dataStreamDummy = null; device.ImmediateContext.CopyResource(texture, textureDummy); DataBox databox = device.ImmediateContext.MapSubresource(textureDummy, 0, 0, MapMode.Read, global::SharpDX.Direct3D11.MapFlags.None, out dataStreamDummy); dataStreamDummy.Dispose(); textureDummy.Dispose(); #endregion target.Dispose(); surface.Dispose(); rtv.Dispose(); return(texture); }
void EnsureResources(global::SharpDX.Direct3D11.Device device, Texture2DDescription description, Rectangle captureRegion, ScreenshotRequest request, bool useSameDeviceForResize = false) { var resizeDevice = useSameDeviceForResize ? device : _device; // Check if _resolvedRT or _finalRT require creation if (_finalRT != null && (_finalRT.Device.NativePointer == device.NativePointer || _finalRT.Device.NativePointer == _device.NativePointer) && _finalRT.Description.Height == captureRegion.Height && _finalRT.Description.Width == captureRegion.Width && _resolvedRT != null && _resolvedRT.Description.Height == description.Height && _resolvedRT.Description.Width == description.Width && (_resolvedRT.Device.NativePointer == device.NativePointer || _resolvedRT.Device.NativePointer == _device.NativePointer) && _resolvedRT.Description.Format == description.Format ) { } else { RemoveAndDispose(ref _query); RemoveAndDispose(ref _resolvedRT); RemoveAndDispose(ref _resolvedSRV); RemoveAndDispose(ref _finalRT); RemoveAndDispose(ref _resolvedRTShared); RemoveAndDispose(ref _resolvedRTKeyedMutex); RemoveAndDispose(ref _resolvedRTKeyedMutex_Dev2); _query = new Query(resizeDevice, new QueryDescription() { Flags = QueryFlags.None, Type = QueryType.Event }); _queryIssued = false; try { ResourceOptionFlags resolvedRTOptionFlags = ResourceOptionFlags.None; if (device != resizeDevice) { resolvedRTOptionFlags |= ResourceOptionFlags.SharedKeyedmutex; } _resolvedRT = ToDispose(new Texture2D(device, new Texture2DDescription() { CpuAccessFlags = CpuAccessFlags.None, Format = description.Format, // for multisampled backbuffer, this must be same format Height = description.Height, Usage = ResourceUsage.Default, Width = description.Width, ArraySize = 1, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), // Ensure single sample BindFlags = BindFlags.ShaderResource, MipLevels = 1, OptionFlags = resolvedRTOptionFlags })); } catch { // Failed to create the shared resource, try again using the same device as game for resize EnsureResources(device, description, captureRegion, request, true); return; } // Retrieve reference to the keyed mutex _resolvedRTKeyedMutex = ToDispose(_resolvedRT.QueryInterfaceOrNull <global::SharpDX.DXGI.KeyedMutex>()); // If the resolvedRT is a shared resource _resolvedRTKeyedMutex will not be null if (_resolvedRTKeyedMutex != null) { using (var resource = _resolvedRT.QueryInterface <global::SharpDX.DXGI.Resource>()) { _resolvedRTShared = ToDispose(resizeDevice.OpenSharedResource <Texture2D>(resource.SharedHandle)); _resolvedRTKeyedMutex_Dev2 = ToDispose(_resolvedRTShared.QueryInterfaceOrNull <global::SharpDX.DXGI.KeyedMutex>()); } // SRV for use if resizing _resolvedSRV = ToDispose(new ShaderResourceView(resizeDevice, _resolvedRTShared)); } else { _resolvedSRV = ToDispose(new ShaderResourceView(resizeDevice, _resolvedRT)); } _finalRT = ToDispose(new Texture2D(resizeDevice, new Texture2DDescription() { CpuAccessFlags = CpuAccessFlags.Read, Format = description.Format, Height = captureRegion.Height, Usage = ResourceUsage.Staging, Width = captureRegion.Width, ArraySize = 1, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), BindFlags = BindFlags.None, MipLevels = 1, OptionFlags = ResourceOptionFlags.None })); _finalRTMapped = false; } if (_resolvedRT != null && _resolvedRTKeyedMutex_Dev2 == null && resizeDevice == _device) { resizeDevice = device; } if (resizeDevice != null && request.Resize != null && (_resizedRT == null || (_resizedRT.Device.NativePointer != resizeDevice.NativePointer || _resizedRT.Description.Width != request.Resize.Value.Width || _resizedRT.Description.Height != request.Resize.Value.Height))) { // Create/Recreate resources for resizing RemoveAndDispose(ref _resizedRT); RemoveAndDispose(ref _resizedRTV); RemoveAndDispose(ref _saQuad); _resizedRT = ToDispose(new Texture2D(resizeDevice, new Texture2DDescription() { Format = global::SharpDX.DXGI.Format.R8G8B8A8_UNorm, // Supports BMP/PNG/etc Height = request.Resize.Value.Height, Width = request.Resize.Value.Width, ArraySize = 1, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), BindFlags = BindFlags.RenderTarget, MipLevels = 1, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.None })); _resizedRTV = ToDispose(new RenderTargetView(resizeDevice, _resizedRT)); _saQuad = ToDispose(new ScreenAlignedQuadRenderer()); _saQuad.Initialize(new DeviceManager(resizeDevice)); } }