/// <summary> /// Function to update the current render target. /// </summary> /// <param name="target">The new target.</param> private void UpdateTarget(ref Gorgon2DTarget target) { // If we currently have a swap chain bound, then we need to unbind its resize event. if (_currentTarget.SwapChain != null) { _currentTarget.SwapChain.AfterSwapChainResized -= target_Resized; } if (!Gorgon2DTarget.Equals(ref target, ref _currentTarget)) { if (target.Target != _currentTarget.Target) { Graphics.Output.SetRenderTarget(target.Target, DepthStencil ?? target.DepthStencil); } _currentTarget = target; // If the current render target is the default render target, then copy the current target settings // into the default. This is just in case we've resized the current target and the default is out // of sync. if (_defaultTarget.Target == _currentTarget.Target) { _defaultTarget = _currentTarget; } } // Update camera. var camera = _camera ?? DefaultCamera; if (camera.AutoUpdate) { camera.ViewDimensions = new RectangleF(Vector2.Zero, new Vector2(_currentTarget.Width, _currentTarget.Height)); } var clipRegion = ClipRegion; // Restore the clipping region. if (clipRegion != null) { ClipRegion = null; ClipRegion = clipRegion; } Graphics.Rasterizer.SetViewport(_viewPort ?? _currentTarget.Viewport); if (_currentTarget.SwapChain == null) { return; } _currentTarget.SwapChain.AfterSwapChainResized += target_Resized; }
/// <summary> /// Function to handle a resize of the current render target. /// </summary> /// <param name="sender">Object that sent the event.</param> /// <param name="e">Event parameters.</param> private void target_Resized(object sender, GorgonAfterSwapChainResizedEventArgs e) { GorgonDepthStencilView depthStencil = _currentTarget.SwapChain.DepthStencilBuffer; if (DepthStencil != null) { depthStencil = DepthStencil; } var target = new Gorgon2DTarget(_currentTarget.SwapChain, depthStencil); UpdateTarget(ref target); }
/// <summary> /// Function to set up the renderers initial state. /// </summary> private void SetDefaultStates() { // Add shader includes if they're gone. if (!Graphics.Shaders.IncludeFiles.Contains("Gorgon2DShaders")) { Graphics.Shaders.IncludeFiles.Add("Gorgon2DShaders", Encoding.UTF8.GetString(Resources.BasicSprite)); } // Record the initial state before set up. if (_initialState == null) { _initialState = new Gorgon2DStateRecall(this); } // Reset the cache values. _cache.Reset(); // Set our default shaders. VertexShader.Current = VertexShader.DefaultVertexShader; PixelShader.Current = PixelShader.DefaultPixelShaderDiffuse; Graphics.Input.IndexBuffer = DefaultIndexBuffer; Graphics.Input.VertexBuffers[0] = DefaultVertexBufferBinding; Graphics.Input.Layout = DefaultLayout; Graphics.Input.PrimitiveType = PrimitiveType.TriangleList; IsMultisamplingEnabled = Graphics.Rasterizer.States.IsMultisamplingEnabled; if (PixelShader != null) { GorgonTextureSamplerStates sampler = GorgonTextureSamplerStates.LinearFilter; sampler.TextureFilter = TextureFilter.Point; Graphics.Shaders.PixelShader.TextureSamplers[0] = sampler; Graphics.Shaders.PixelShader.Resources[0] = null; } Graphics.Rasterizer.States = GorgonRasterizerStates.CullBackFace; Graphics.Output.BlendingState.States = GorgonBlendStates.DefaultStates; Graphics.Output.DepthStencilState.States = GorgonDepthStencilStates.NoDepthStencil; Graphics.Output.DepthStencilState.StencilReference = 0; Graphics.Output.SetRenderTarget(_defaultTarget.Target, _defaultTarget.DepthStencil); _currentTarget = _defaultTarget; UpdateTarget(ref _currentTarget); DefaultCamera.Update(); // Get the current state. DefaultState = new Gorgon2DStateRecall(this); // By default, turn on multi sampling over a count of 1. if (Target.Resource.ResourceType != ResourceType.Texture2D) { return; } var target2D = (GorgonRenderTarget2D)Target.Resource; if ((!IsMultisamplingEnabled) && ((target2D.Settings.Multisampling.Count > 1) || (target2D.Settings.Multisampling.Quality > 0)) && ((Graphics.VideoDevice.SupportedFeatureLevel == DeviceFeatureLevel.SM4_1) || (Graphics.VideoDevice.SupportedFeatureLevel == DeviceFeatureLevel.SM5))) { _multiSampleEnable = true; } }
/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> private void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { // Dump any pending rendering. _cache.Reset(); if (_initialState != null) { _initialState.Restore(true); _initialState = null; } TrackedObjects.ReleaseAll(); if (Effects != null) { Effects.FreeEffects(); Effects = null; } if (_currentTarget.SwapChain != null) { _currentTarget.SwapChain.AfterSwapChainResized -= target_Resized; } if (DefaultLayout != null) { DefaultLayout.Dispose(); } if (VertexShader != null) { VertexShader.CleanUp(); } if (PixelShader != null) { PixelShader.CleanUp(); } VertexShader = null; PixelShader = null; DefaultVertexBufferBinding.VertexBuffer.Dispose(); if (DefaultIndexBuffer != null) { DefaultIndexBuffer.Dispose(); } if ((_systemCreatedTarget) && (_defaultTarget.Target != null)) { _defaultTarget.Target.Resource.Dispose(); _defaultTarget = default(Gorgon2DTarget); } Graphics.RemoveTrackedObject(this); } _disposed = true; }