<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

using Duality.Drawing;

using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Duality.Backend.DefaultOpenTK
{
	[DontSerialize]
	public class NativeRenderTarget : INativeRenderTarget
	{
		private static int maxFboSamples = -1;
		public static int MaxRenderTargetSamples
		{
			get 
			{
				if (maxFboSamples == -1) GL.GetInteger(GetPName.MaxSamples, out maxFboSamples);
				return maxFboSamples;
			}
		}
		
		private	static NativeRenderTarget curBound = null;
		public static NativeRenderTarget BoundRT
		{
			get { return curBound; }
		}
		public static void Bind(NativeRenderTarget nextBound)
		{
			if (curBound == nextBound) return;

			if (curBound != null && nextBound != curBound)
			{
				// Blit multisampled fbo
				if (curBound.samples > 0)
				{
					GL.Ext.BindFramebuffer(FramebufferTarget.ReadFramebuffer, curBound.handleMsaaFBO);
					GL.Ext.BindFramebuffer(FramebufferTarget.DrawFramebuffer, curBound.handleMainFBO);
					for (int i = 0; i < curBound.targetInfos.Count; i++)
					{
						GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + i));
						GL.DrawBuffer((DrawBufferMode)((int)DrawBufferMode.ColorAttachment0 + i));
						GL.Ext.BlitFramebuffer(
							0, 0, curBound.targetInfos.Data[i].Target.Width, curBound.targetInfos.Data[i].Target.Height,
							0, 0, curBound.targetInfos.Data[i].Target.Width, curBound.targetInfos.Data[i].Target.Height,
							ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);
					}
					GL.ReadBuffer(ReadBufferMode.Back);
					GL.DrawBuffer(DrawBufferMode.Back);
					GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
				}

				// Generate Mipmaps for last bound
				for (int i = 0; i < curBound.targetInfos.Count; i++)
				{
					if (curBound.targetInfos.Data[i].Target.HasMipmaps)
					{
						GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);<commit_msg>RenderTarget Bugfixes #FIX: Fixed an OpenGL error in the backend that could occur when changing the sample count of a RenderTarget while in use.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

using Duality.Drawing;

using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Duality.Backend.DefaultOpenTK
{
	[DontSerialize]
	public class NativeRenderTarget : INativeRenderTarget
	{
		private static int maxFboSamples = -1;
		public static int MaxRenderTargetSamples
		{
			get 
			{
				if (maxFboSamples == -1) GL.GetInteger(GetPName.MaxSamples, out maxFboSamples);
				return maxFboSamples;
			}
		}
		
		private	static NativeRenderTarget curBound = null;
		public static NativeRenderTarget BoundRT
		{
			get { return curBound; }
		}
		public static void Bind(NativeRenderTarget nextBound)
		{
			if (curBound == nextBound) return;

			if (curBound != null && nextBound != curBound)
			{
				// Blit multisampled fbo
				if (curBound.samples > 0)
				{
					GL.Ext.BindFramebuffer(FramebufferTarget.ReadFramebuffer, curBound.handleMsaaFBO);
					GL.Ext.BindFramebuffer(FramebufferTarget.DrawFramebuffer, curBound.handleMainFBO);
					for (int i = 0; i < curBound.targetInfos.Count; i++)
					{
						GL.ReadBuffer((ReadBufferMode)((int)ReadBufferMode.ColorAttachment0 + i));
						GL.DrawBuffer((DrawBufferMode)((int)DrawBufferMode.ColorAttachment0 + i));
						GL.Ext.BlitFramebuffer(
							0, 0, curBound.targetInfos.Data[i].Target.Width, curBound.targetInfos.Data[i].Target.Height,
							0, 0, curBound.targetInfos.Data[i].Target.Width, curBound.targetInfos.Data[i].Target.Height,
							ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);
					}
					GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
					GL.ReadBuffer(ReadBufferMode.Back);
					GL.DrawBuffer(DrawBufferMode.Back);
				}

				// Generate Mipmaps for last bound
				for (int i = 0; i < curBound.targetInfos.Count; i++)
				{
					if (curBound.targetInfos.Data[i].Target.HasMipmaps)
					{
						GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
{'fraction_non_alphanumeric': 0.1057431599704215, 'fraction_numerical': 0.006408676361843727, 'mean_word_length': 3.9068923821039903, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3316427', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1305, 'n_words': 252}