/// <summary> /// Loads the resource. /// </summary> /// <param name="device">The device.</param> /// <param name="resources">Parent ResourceDictionary.</param> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { m_defaultResources = resources.DefaultResources; m_vertexShader = resources.GetResourceAndEnsureLoaded( RES_KEY_VERTEX_SHADER, () => GraphicsHelper.GetVertexShaderResource(device, "Postprocessing", "PostprocessVertexShader")); }
/// <summary> /// Loads the resource. /// </summary> /// <param name="device">The device.</param> /// <param name="resources">Parent ResourceDictionary.</param> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { base.LoadResourceInternal(device, resources); // Load graphics resources m_pixelShaderBlur = resources.GetResourceAndEnsureLoaded( RES_KEY_PIXEL_SHADER_BLUR, () => GraphicsHelper.GetPixelShaderResource(device, "Postprocessing", "PostprocessBlur")); m_singleForcedColor = resources.GetResourceAndEnsureLoaded <SingleForcedColorMaterialResource>( KEY_MATERIAL, () => new SingleForcedColorMaterialResource() { FadeIntensity = m_fadeIntensity }); m_renderTarget = resources.GetResourceAndEnsureLoaded <RenderTargetTextureResource>( KEY_RENDER_TARGET, () => new RenderTargetTextureResource(RenderTargetCreationMode.Color)); m_defaultResources = resources.DefaultResources; // Load constant buffers m_cbFirstPass = resources.GetResourceAndEnsureLoaded <TypeSafeConstantBufferResource <CBPerObject> >( KEY_CB_PASS_01, () => new TypeSafeConstantBufferResource <CBPerObject>(new CBPerObject() { BlurIntensity = 0.0f, BlurOpacity = 0.1f })); m_cbSecondPass = resources.GetResourceAndEnsureLoaded <TypeSafeConstantBufferResource <CBPerObject> >( KEY_CB_PASS_02, () => new TypeSafeConstantBufferResource <CBPerObject>(new CBPerObject() { BlurIntensity = 0.8f, BlurOpacity = 0.5f })); }
/// <summary> /// Draws the given frame to the video. /// </summary> /// <param name="device">The device on which the given framebuffer is created.</param> /// <param name="uploadedTexture">The texture which should be added to the video.</param> public void DrawFrame(EngineDevice device, MemoryMappedTexture <int> uploadedTexture) { try { device.EnsureNotNull(nameof(device)); uploadedTexture.EnsureNotNull(nameof(uploadedTexture)); if (!_hasStarted) { throw new SeeingSharpGraphicsException($"{nameof(SeeingSharpVideoWriter)} is not started!"); } if (_hasFinished) { throw new SeeingSharpGraphicsException($"{nameof(SeeingSharpVideoWriter)} has already finished before!"); } // Check for correct image size if (_videoSize != uploadedTexture.PixelSize) { throw new SeeingSharpGraphicsException("Size has changed during recording!"); } this.DrawFrameInternal(device, uploadedTexture); } catch (Exception ex) { _drawException = ex; } }
/// <summary> /// Initializes a new instance of the <see cref="Direct2DOverlayRenderer"/> class. /// </summary> internal Direct2DOverlayRenderer(EngineDevice device, D3D11.ID3D11Texture2D renderTarget3D, int viewWidth, int viewHeight, DpiScaling dpiScaling, bool forceInit) { _device = device; _renderTarget3D = renderTarget3D; this.CreateResources(viewWidth, viewHeight, dpiScaling, forceInit); }
public IDevice CreateDevice(ILogger logger, ITransportFactory transportFactory, ITelemetryFactory telemetryFactory, IConfigurationProvider configurationProvider, InitialDeviceConfig config) { var device = new EngineDevice(logger, transportFactory, telemetryFactory, configurationProvider); device.Init(config); return device; }
/// <summary> /// Gets the bitmap for the given device.. /// </summary> /// <param name="engineDevice">The engine device.</param> internal override D2D.ID2D1Bitmap GetBitmap(EngineDevice engineDevice) { // Check for disposed state if (this.IsDisposed) { throw new ObjectDisposedException(this.GetType().Name); } GraphicsCore.EnsureGraphicsSupportLoaded(); var result = _loadedBitmaps[engineDevice.DeviceIndex]; if (result == null) { using var mapped = new MemoryMappedTexture <int>(_bitmapSize); // Load the bitmap initially result = engineDevice.FakeRenderTarget2D !.CreateBitmap( new Size(_bitmapSize.Width, _bitmapSize.Height), mapped.Pointer, mapped.Width * 4, new D2D.BitmapProperties(_pixelFormat, (float)_dpiX, (float)_dpiY)); _loadedBitmaps[engineDevice.DeviceIndex] = result; engineDevice.RegisterDeviceResource(this); } return(result); }
/// <summary> /// Unloads all resources. /// </summary> /// <param name="device">The device on which the resources where loaded.</param> /// <param name="resources">The current ResourceDictionary.</param> protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources) { m_graphics2D = null; GraphicsHelper.SafeDispose(ref m_overlayRenderer); GraphicsHelper.SafeDispose(ref m_renderTargetTextureView); GraphicsHelper.SafeDispose(ref m_renderTargetTexture); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!SeeingSharpApplication.IsInitialized) { return; } // Create main ViewModel m_sceneManager = new SceneManager(m_panGraphics.RenderLoop); m_sceneManager.PropertyChanged += OnSceneManager_PropertyChanged; // Update Input/Export formats for file dialog m_dlgOpenFile.Filter = GraphicsCore.Current.ImportersAndExporters.GetOpenFileDialogFilter(); // Handle device combobox m_cboDevice.Text = GraphicsCore.Current.DefaultDevice.AdapterDescription; foreach (EngineDevice actDevice in GraphicsCore.Current.LoadedDevices) { EngineDevice actDeviceInner = actDevice; m_cboDevice.DropDownItems.Add( actDevice.AdapterDescription, null, (sender, eArgs) => { m_panGraphics.RenderLoop.SetRenderingDevice(actDeviceInner); m_cboDevice.Text = actDeviceInner.AdapterDescription; }); } // Peform common updates finally this.UpdateDialogState(); }
/// <summary> /// Loads the resource. /// </summary> /// <param name="device">The target device.</param> /// <param name="resources">Parent ResourceDictionary.</param> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { //Get default resources _defaultResources = resources.GetResourceAndEnsureLoaded( DefaultResources.RESOURCE_KEY, () => new DefaultResources()); }
/// <summary> /// Gets the brush for the given device. /// </summary> /// <param name="engineDevice">The device for which to get the brush.</param> internal override D2D.Brush GetBrush(EngineDevice engineDevice) { // Check for disposed state if (base.IsDisposed) { throw new ObjectDisposedException(this.GetType().Name); } D2D.SolidColorBrush result = m_loadedBrushes[engineDevice.DeviceIndex]; if (result == null) { // Load the brush result = new D2D.SolidColorBrush( engineDevice.FakeRenderTarget2D, m_singleColor.ToDXColor(), new D2D.BrushProperties() { Opacity = m_opacity, Transform = Matrix3x2.Identity.ToDXMatrix() }); m_loadedBrushes[engineDevice.DeviceIndex] = result; } return(result); }
/// <summary> /// Loads all resources of the object. /// </summary> /// <param name="device">Current graphics device.</param> /// <param name="resourceDictionary">Current resource dicionary.</param> public override void LoadResources(EngineDevice device, ResourceDictionary resourceDictionary) { m_localResources.AddObject( resourceDictionary.GetResourceAndEnsureLoaded <GeometryResource>(m_resGeometryKey), device.DeviceIndex, false); }
/// <summary> /// Gets the input object for an effect. /// </summary> /// <param name="device">The device for which to get the input.</param> IDisposable IImageInternal.GetImageObject(EngineDevice device) { if (device.IsUsingFallbackMethodFor2D) { return(null); } D2D.Effect effect = m_loadedEffects[device.DeviceIndex]; if (effect == null) { // Create the effect effect = BuildEffect(device); // Set input values for (int loop = 0; loop < m_effectInputs.Length; loop++) { using (D2D.Image actInput = m_effectInputs[loop].GetImageObject(device) as D2D.Image) { effect.SetInput(loop, actInput, new SharpDX.Mathematics.Interop.RawBool(false)); } } // Store loaded effect m_loadedEffects[device.DeviceIndex] = effect; } return(effect.Output); }
/// <summary> /// Called when RenderLoop object checks wheter it is possible to render. /// </summary> private bool OnRenderLoopCheckCanRender(EngineDevice engineDevice) { if (m_targetPanel == null) { return(false); } if (m_targetPanel.ActualWidth <= 0) { return(false); } if (m_targetPanel.ActualHeight <= 0) { return(false); } if (m_drawingInterop == null) { return(false); } if (m_drawingInterop.FrameRequestCountBeforeRender > 0) { return(false); } return(true); }
/// <summary> /// Disposes all loaded view resources. /// </summary> private void OnRenderLoopDisposeViewResources(EngineDevice engineDevice) { m_renderTargetDepth = GraphicsHelper.DisposeObject(m_renderTargetDepth); m_depthBuffer = GraphicsHelper.DisposeObject(m_depthBuffer); m_renderTargetView = GraphicsHelper.DisposeObject(m_renderTargetView); m_backBuffer = GraphicsHelper.DisposeObject(m_backBuffer); }
/// <summary> /// Loads the resource. /// </summary> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { _cbPerObject = resources.GetResourceAndEnsureLoaded( _keyConstantBuffer, () => new TypeSafeConstantBufferResource <CBPerObject>()); NeedsRefresh = true; }
/// <summary> /// Loads the resource. /// </summary> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { // Select source texture var source = _resourceLinkLowQuality; if (device.Configuration.TextureQuality == TextureQuality.High) { source = _resourceLinkHighQuality; } // Load the texture if (source != null) { _texture = GraphicsHelper.CreateTexture(device, source); } else { _texture = GraphicsHelper.Internals.LoadTexture2DFromMappedTexture(device, _inMemoryTexture !, true); } // Create view for shaders _textureView = device.DeviceD3D11_1.CreateShaderResourceView(_texture); // Some checking.. _isCubeTexture = _texture.Description.ArraySize == 6 && (_texture.Description.OptionFlags & D3D11.ResourceOptionFlags.TextureCube) == D3D11.ResourceOptionFlags.TextureCube; _isRenderTarget = (_texture.Description.BindFlags & D3D11.BindFlags.RenderTarget) == D3D11.BindFlags.RenderTarget; }
/// <summary> /// Converts a System.Drawing.Bitmap to a DirectX 11 texture object. /// </summary> /// <param name="device">Device on which the resource should be created.</param> /// <param name="bitmap">The source bitmap.</param> internal static D3D11.ID3D11Texture2D LoadTextureFromBitmap(EngineDevice device, GDI.Bitmap bitmap) { device.EnsureNotNull(nameof(device)); bitmap.EnsureNotNull(nameof(bitmap)); return(LoadTextureFromBitmap(device, bitmap, 1)); }
/// <summary> /// Loads the resource. /// </summary> protected internal override void LoadShader(EngineDevice device, byte[] shaderBytecode) { if (m_vertexShader == null) { m_vertexShader = new D3D11.VertexShader(device.DeviceD3D11_1, shaderBytecode); } }
/// <inheritdoc /> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { // Build geometries Geometry[] geometries = { _geometry.BuildGeometry(new GeometryBuildOptions(device.SupportedDetailLevel)) }; // Build BoundingBox around all vertices var vertexLocations = new List <Vector3>(); for (var loop = 0; loop < geometries.Length; loop++) { foreach (var actVertex in geometries[loop].Vertices) { vertexLocations.Add(actVertex.Position); } } _boundingBox = new BoundingBox(vertexLocations); // Build geometry _chunkTemplates = BuildBuffers(device, geometries); // Update counters this.LoadedGeometryTriangleCount = 0; this.LoadedGeometryRenderingChunkCount = _chunkTemplates.Length; for (var loop = 0; loop < _chunkTemplates.Length; loop++) { this.LoadedGeometryTriangleCount += (_chunkTemplates[loop].IndexCount / 3); } }
/// <summary> /// Unloads the resource. /// </summary> protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources) { m_vertexShader = null; m_pixelShader = null; m_textureResource = null; m_cbPerMaterial = null; }
void IRenderLoopHost.OnRenderLoop_PrepareRendering(EngineDevice engineDevice) { if ((m_targetPanel != null) && (m_renderLoop != null) && (m_renderLoop.Camera != null) && (m_swapChain != null)) { // Update swap chain scaling (only relevant for SwapChainPanel targets) // see https://www.packtpub.com/books/content/integrating-direct3d-xaml-and-windows-81 if (m_compositionScaleChanged && m_targetPanel.CompositionRescalingNeeded) { m_compositionScaleChanged = false; DXGI.SwapChain2 swapChain2 = m_swapChain.QueryInterfaceOrNull <DXGI.SwapChain2>(); if (swapChain2 != null) { try { SharpDX.Matrix3x2 inverseScale = new SharpDX.Matrix3x2(); inverseScale.M11 = 1.0f / (float)m_targetPanel.CompositionScaleX; inverseScale.M22 = 1.0f / (float)m_targetPanel.CompositionScaleY; swapChain2.MatrixTransform = inverseScale; } finally { swapChain2.Dispose(); } } } } }
/// <inheritdoc /> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { base.LoadResourceInternal(device, resources); // Load graphics resources _pixelShaderBlur = resources.GetResourceAndEnsureLoaded( s_resKeyPixelShaderBlur, () => GraphicsHelper.Internals.GetPixelShaderResource(device, "Postprocessing", "PostprocessBlur")); _singleForcedColor = resources.GetResourceAndEnsureLoaded( s_keyMaterial, () => new SingleForcedColorMaterialResource { FadeIntensity = _fadeIntensity }); _renderTarget = resources.GetResourceAndEnsureLoaded( s_keyRenderTarget, () => new RenderTargetTextureResource(RenderTargetCreationMode.Color)); _defaultResources = resources.DefaultResources; // Load constant buffers _cbFirstPass = resources.GetResourceAndEnsureLoaded( _keyCbPass01, () => new TypeSafeConstantBufferResource <CbPerObject>(new CbPerObject { BlurIntensity = 0.0f, BlurOpacity = 0.1f })); _cbSecondPass = resources.GetResourceAndEnsureLoaded( _keyCbPass02, () => new TypeSafeConstantBufferResource <CbPerObject>(new CbPerObject { BlurIntensity = 0.8f, BlurOpacity = 0.5f })); }
/// <summary> /// Unloads the resource. /// </summary> protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources) { SeeingSharpUtil.SafeDispose(ref _inputLayout); _vertexShader = null; _pixelShader = null; }
/// <summary> /// Gets the brush for the given device. /// </summary> /// <param name="engineDevice">The device for which to get the brush.</param> internal override D2D.ID2D1Brush GetBrush(EngineDevice engineDevice) { // Check for disposed state if (this.IsDisposed) { throw new ObjectDisposedException(this.GetType().Name); } var result = _loadedBrushes[engineDevice.DeviceIndex]; if (result == null) { // Load the brush result = engineDevice.FakeRenderTarget2D !.CreateSolidColorBrush( MathConverter.RawFromColor4(this.Color), new D2D.BrushProperties { Opacity = this.Opacity, Transform = Matrix3x2.Identity }); _loadedBrushes[engineDevice.DeviceIndex] = result; engineDevice.RegisterDeviceResource(this); } return(result); }
/// <summary> /// Unloads all resources. /// </summary> /// <param name="device">The device on which the resources where loaded.</param> /// <param name="resources">The current ResourceDictionary.</param> protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources) { GraphicsHelper.SafeDispose(ref m_videoReader); GraphicsHelper.SafeDispose(ref m_textureView); GraphicsHelper.SafeDispose(ref m_texture); }
/// <summary> /// Loads the resource. /// </summary> /// <param name="device">The device.</param> /// <param name="resources">Parent ResourceDictionary.</param> protected override void LoadResourceInternal(EngineDevice device, ResourceDictionary resources) { _defaultResources = resources.DefaultResources; _vertexShader = resources.GetResourceAndEnsureLoaded( s_resKeyVertexShader, () => GraphicsHelper.Internals.GetVertexShaderResource(device, "Postprocessing", "PostprocessVertexShader")); }
/// <summary> /// Unloads the resource. /// </summary> protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources) { _defaultResources = null; _vertexShader = null; _pixelShader = null; _textureResource = null; }
/// <summary> /// Creates an immutable vertex buffer from the given vertex array. /// </summary> /// <typeparam name="T">Type of a vertex.</typeparam> /// <param name="device">Graphics device.</param> /// <param name="vertices">The vertex array.</param> public static unsafe D3D11.ID3D11Buffer CreateImmutableVertexBuffer <T>(EngineDevice device, params T[][] vertices) where T : unmanaged { device.EnsureNotNull(nameof(device)); vertices.EnsureNotNull(nameof(vertices)); var vertexCount = vertices.Sum(actArray => actArray.Length); var vertexSize = sizeof(T); using var outStream = new DataStream( vertexCount * vertexSize, true, true); foreach (var actArray in vertices) { outStream.WriteRange(actArray); } outStream.Position = 0; var bufferDescription = new D3D11.BufferDescription { BindFlags = D3D11.BindFlags.VertexBuffer, CpuAccessFlags = D3D11.CpuAccessFlags.None, OptionFlags = D3D11.ResourceOptionFlags.None, SizeInBytes = vertexCount * vertexSize, Usage = D3D11.ResourceUsage.Immutable, StructureByteStride = vertexSize }; var result = device.DeviceD3D11_1.CreateBuffer(bufferDescription, outStream.DataPointer); return(result); }
/// <summary> /// Create a new DepthBuffer view to bind the given depth buffer to the rendering device. /// </summary> /// <param name="device">The device on which to create the view.</param> /// <param name="depthBuffer">The target resource.</param> public static D3D11.ID3D11DepthStencilView CreateDepthBufferView(EngineDevice device, D3D11.ID3D11Texture2D depthBuffer) { device.EnsureNotNull(nameof(device)); depthBuffer.EnsureNotNullOrDisposed(nameof(depthBuffer)); return(device.DeviceD3D11_1.CreateDepthStencilView(depthBuffer)); }
/// <summary> /// Creates a staging texture which enables copying data from gpu to cpu memory. /// </summary> /// <param name="device">Graphics device.</param> /// <param name="width">Width of generated texture.</param> /// <param name="height">Height of generated texture.</param> public static D3D11.ID3D11Texture2D CreateStagingTexture(EngineDevice device, int width, int height) { device.EnsureNotNull(nameof(device)); width.EnsurePositiveOrZero(nameof(width)); height.EnsurePositiveOrZero(nameof(height)); //For handling of staging resource see // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259(v=vs.85).aspx var textureDescription = new D3D11.Texture2DDescription { Width = width, Height = height, MipLevels = 1, ArraySize = 1, Format = DEFAULT_TEXTURE_FORMAT, Usage = D3D11.ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0), BindFlags = D3D11.BindFlags.None, CpuAccessFlags = D3D11.CpuAccessFlags.Read, OptionFlags = D3D11.ResourceOptionFlags.None }; return(device.DeviceD3D11_1.CreateTexture2D(textureDescription)); }
/// <summary> /// Loads the resource. /// </summary> protected internal override void LoadShader(EngineDevice device, byte[] shaderBytecode) { if (_pixelShader == null) { _pixelShader = device.DeviceD3D11_1.CreatePixelShader(shaderBytecode); } }
public StartCommandProcessor(EngineDevice device) : base(device) { }