void CL_GL1FrameBufferProvider::attach_color_buffer(int color_buffer, const CL_Texture &texture, int level, int zoffset)
{
	if(!pbuffer.is_null())
		sync_texture();

	if (texture.is_null())
	{
		return;
	}

	CL_GL1TextureProvider *texture_provider = dynamic_cast<CL_GL1TextureProvider *> (texture.get_provider());
	if (texture_provider == NULL)
	{
		throw CL_Exception("Selected texture is not a GL1 texture");
	}

	CL_Size surface_size = texture_provider->get_surface_size();

	// Find existing pbuffer
	CL_WeakPtr<CL_Texture_Impl> texture_impl(texture.get_impl());
	std::map< CL_WeakPtr<CL_Texture_Impl>, CL_PBuffer_GL1>::iterator texture_it = texture_pbuffer_map.find(texture_impl);
	if (texture_it == texture_pbuffer_map.end())
	{
		// Not found, create a new entry
		pbuffer = gc_provider->create_pbuffer(surface_size);
		texture_pbuffer_map[texture_impl] = pbuffer;
	}
	else
	{
		// Used cached pbuffer
		pbuffer = texture_it->second;
	}

	selected_texture_provider = texture_provider;
	selected_surface = texture;

	sync_pbuffer();

	// Purge the cache from unused pbuffers. Maybe we could reuse them instead? But for this to occur, the user would be recreating CL_Textures anyway (which is slow). This added complexity is not required (at the moment)
	for (texture_it = texture_pbuffer_map.begin(); texture_it != texture_pbuffer_map.end();)
	{
		if (texture_it->first.is_null())
		{
			// This "texture_it = texture_pbuffer_map.erase(texture_it);" works on visual studio,
			// but MSDN says This return type does not conform to the C++ standard. So do it a different way, so GCC does not complain
			texture_pbuffer_map.erase(texture_it);
			texture_it = texture_pbuffer_map.begin();
		}
		else
		{
			++texture_it;
		}
	}
}
	void GL1FrameBufferProvider::attach_color(int attachment_index, const Texture2D &texture, int level)
	{
		throw_if_disposed();

		if (!pbuffer.is_null())
			sync_texture();

		GL1TextureProvider *texture_provider = dynamic_cast<GL1TextureProvider *> (texture.get_provider());
		if (texture_provider == nullptr)
		{
			throw Exception("Selected texture is not a GL1 texture");
		}

		Size surface_size = texture_provider->get_surface_size();

		// Find existing pbuffer
		std::weak_ptr<Texture_Impl> texture_impl(texture.get_impl());
		auto texture_it = texture_pbuffer_map.find(texture_impl);
		if (texture_it == texture_pbuffer_map.end())
		{
			// Not found, create a new entry
			pbuffer = PBuffer_GL1(gc_provider);
			pbuffer.create(gc_provider->get_opengl_window(), surface_size);
			texture_pbuffer_map[texture_impl] = pbuffer;
		}
		else
		{
			// Used cached pbuffer
			pbuffer = texture_it->second;
		}

		selected_texture_provider = texture_provider;
		selected_surface = texture;

		sync_pbuffer();

		// Purge the cache from unused pbuffers. Maybe we could reuse them instead? But for this to occur, the user would be recreating Textures anyway (which is slow). This added complexity is not required (at the moment)
		for (texture_it = texture_pbuffer_map.begin(); texture_it != texture_pbuffer_map.end();)
		{
			if (texture_it->first.expired())
			{
				// This "texture_it = texture_pbuffer_map.erase(texture_it);" works on visual studio,
				// but MSDN says This return type does not conform to the C++ standard. So do it a different way, so GCC does not complain
				texture_pbuffer_map.erase(texture_it);
				texture_it = texture_pbuffer_map.begin();
			}
			else
			{
				++texture_it;
			}
		}
	}