<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SlimDX;
using SlimDX.DXGI;
using SlimDX.Direct3D11;

using SRPRendering;
using ShaderEditorApp.ViewModel;

namespace ShaderEditorApp.View
{

	public class RenderWindow : Control
	{
		private List<IDisposable> resources = new List<IDisposable>();
		private List<IDisposable> sizeDependentResources = new List<IDisposable>();

		private readonly SlimDX.Direct3D11.Device _device;
		private readonly SlimDX.DXGI.SwapChain swapChain;

		private readonly WorkspaceViewModel _workspaceVM;

		private RenderTargetView renderTarget;
		private DepthBuffer depthBuffer;

		private Camera camera;

		private bool bNeedsRepaint = false;

		public ViewportViewModel ViewportViewModel { get; }

		public RenderWindow(SlimDX.Direct3D11.Device device, WorkspaceViewModel workspaceVM)
		{
			_device = device;
			_workspaceVM = workspaceVM;
		
			// Create camera.
			ViewportViewModel = new ViewportViewModel();
			camera = new Camera(this, ViewportViewModel);

			MouseClick += RenderWindow_MouseClick;

			// Initialise D3D11 swap chain.
			var description = new SwapChainDescription()
			{
				BufferCount = 1,
				Usage = Usage.RenderTargetOutput,
				OutputHandle = Handle,
				IsWindowed = true,
				ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
				SampleDescription = new SampleDescription(1, 0),
				Flags = SwapChainFlags.AllowModeSwitch,
				SwapEffect = SwapEffect.Discard
			};

			swapChain = new SwapChain(device.Factory, device, description);
			resources.Add(swapChain);

			// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
			device.Factory.SetWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter);

			this.SizeChanged += OnResize;

			// Redraw viewports when required.
			_workspaceVM.Workspace.RedrawRequired.Subscribe(_ => Invalidate());
		}

		private void OnResize(object sender, EventArgs e)
		{
			// Release any existing resources.
			foreach (var obj in sizeDependentResources)
				obj.Dispose();
			sizeDependentResources.Clear();

			swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
			using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(swapChain, 0))
				renderTarget = new RenderTargetView(_device, resource);
			sizeDependentResources.Add(renderTarget);

			// Create depth/stencil buffer texture.
			depthBuffer = new DepthBuffer(_device, Width, Height);

			sizeDependentResources.Add(depthBuffer);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				// clean up all resources
				foreach (var resource in sizeDependentResources)
					resource.Dispose();
				foreach (var resource in resources)
					resource.Dispose();

				sizeDependentResources.Clear();
				resources.Clear();
			}

			base.Dispose(disposing);
		}

		private void Render()
		{
			var context = _device.ImmediateContext;

			// Clear depth buffer.
			context.ClearDepthStencilView(depthBuffer.DSV, DepthStencilClearFlags.Depth, 1.0f, 0);

			// Construct view info object.
			var viewInfo = new ViewInfo(
				camera.WorldToViewMatrix,
				camera.GetViewToProjectionMatrix(AspectRatio),
				camera.EyePosition,
				camera.Near,
				camera.Far,
				ClientSize.Width,
				ClientSize.Height,
				renderTarget,
				depthBuffer
				);

			_workspaceVM.Workspace.Renderer?.Render(context, viewInfo);

			swapChain.Present(0, PresentFlags.None);
		}

		public void Tick()
		{
			if (bNeedsRepaint || _workspaceVM.RealTimeMode)
			{
				bNeedsRepaint = false;
				Render();
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			bNeedsRepaint = true;
		}

		// Override background paint event to prevent flickering.
		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			// Do nothing -- Render() clears the whole window.
		}

		private float AspectRatio => (float)ClientSize.Width / (float)ClientSize.Height;

		void RenderWindow_MouseClick(object sender, MouseEventArgs e)
		{
			// Acquire focus when we're clicked on.
			Focus();
		}
	}
}
<commit_msg>Fixed exception resizing swap chain<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SlimDX;
using SlimDX.DXGI;
using SlimDX.Direct3D11;

using SRPRendering;
using ShaderEditorApp.ViewModel;

namespace ShaderEditorApp.View
{

	public class RenderWindow : Control
	{
		private List<IDisposable> resources = new List<IDisposable>();
		private List<IDisposable> sizeDependentResources = new List<IDisposable>();

		private readonly SlimDX.Direct3D11.Device _device;
		private readonly SlimDX.DXGI.SwapChain swapChain;

		private readonly WorkspaceViewModel _workspaceVM;

		private RenderTargetView renderTarget;
		private DepthBuffer depthBuffer;

		private Camera camera;

		private bool bNeedsRepaint = false;

		public ViewportViewModel ViewportViewModel { get; }

		public RenderWindow(SlimDX.Direct3D11.Device device, WorkspaceViewModel workspaceVM)
		{
			_device = device;
			_workspaceVM = workspaceVM;
		
			// Create camera.
			ViewportViewModel = new ViewportViewModel();
			camera = new Camera(this, ViewportViewModel);

			MouseClick += RenderWindow_MouseClick;

			// Initialise D3D11 swap chain.
			var description = new SwapChainDescription()
			{
				BufferCount = 1,
				Usage = Usage.RenderTargetOutput,
				OutputHandle = Handle,
				IsWindowed = true,
				ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
				SampleDescription = new SampleDescription(1, 0),
				Flags = SwapChainFlags.AllowModeSwitch,
				SwapEffect = SwapEffect.Discard
			};

			swapChain = new SwapChain(device.Factory, device, description);
			resources.Add(swapChain);

			// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
			device.Factory.SetWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter);

			this.SizeChanged += OnResize;

			// Redraw viewports when required.
			_workspaceVM.Workspace.RedrawRequired.Subscribe(_ => Invalidate());
		}

		private void OnResize(object sender, EventArgs e)
		{
			// Release any existing resources.
			foreach (var obj in sizeDependentResources)
				obj.Dispose();
			sizeDependentResources.Clear();

			swapChain.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.AllowModeSwitch);
			using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(swapChain, 0))
				renderTarget = new RenderTargetView(_device, resource);
			sizeDependentResources.Add(renderTarget);

			// Create depth/stencil buffer texture.
			depthBuffer = new DepthBuffer(_device, Width, Height);

			sizeDependentResources.Add(depthBuffer);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				// clean up all resources
				foreach (var resource in sizeDependentResources)
					resource.Dispose();
				foreach (var resource in resources)
					resource.Dispose();

				sizeDependentResources.Clear();
				resources.Clear();
			}

			base.Dispose(disposing);
		}

		private void Render()
		{
			var context = _device.ImmediateContext;

			// Clear depth buffer.
			context.ClearDepthStencilView(depthBuffer.DSV, DepthStencilClearFlags.Depth, 1.0f, 0);

			// Construct view info object.
			var viewInfo = new ViewInfo(
				camera.WorldToViewMatrix,
				camera.GetViewToProjectionMatrix(AspectRatio),
				camera.EyePosition,
				camera.Near,
				camera.Far,
				ClientSize.Width,
				ClientSize.Height,
				renderTarget,
				depthBuffer
				);

			_workspaceVM.Workspace.Renderer?.Render(context, viewInfo);

			swapChain.Present(0, PresentFlags.None);
		}

		public void Tick()
		{
			if (bNeedsRepaint || _workspaceVM.RealTimeMode)
			{
				bNeedsRepaint = false;
				Render();
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			bNeedsRepaint = true;
		}

		// Override background paint event to prevent flickering.
		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			// Do nothing -- Render() clears the whole window.
		}

		private float AspectRatio => (float)ClientSize.Width / (float)ClientSize.Height;

		void RenderWindow_MouseClick(object sender, MouseEventArgs e)
		{
			// Acquire focus when we're clicked on.
			Focus();
		}
	}
}

{'fraction_non_alphanumeric': 0.09044871039924626, 'fraction_numerical': 0.008950653633258745, 'mean_word_length': 4.015948021264029, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5268866', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2527, 'n_words': 606}