@@ -43,55 +43,59 @@ frame_buffer::frame_buffer(
4343		std::move(stencil)
4444	)
4545{
46- 	glGenFramebuffers (1 , &this ->fbo );
47- 	assert_opengl_no_error ();
46+ 	//  In OpenGL framebuffer objects are not shared between contexts,
47+ 	//  so make sure the owning context is bound when deleting the framebuffer object.
48+ 	this ->rendering_context .get ().apply ([this ]() {
49+ 		glGenFramebuffers (1 , &this ->fbo );
50+ 		assert_opengl_no_error ();
4851
49- 	//  No need to initialize the variable because it is initialized via
50- 	//  output argument of glGetIntegerv().
51- 	//  NOLINTNEXTLINE(cppcoreguidelines-init-variables)
52- 	GLint old_fb;
53- 	glGetIntegerv (GL_FRAMEBUFFER_BINDING, &old_fb);
52+ 		 //  No need to initialize the variable because it is initialized via
53+ 		 //  output argument of glGetIntegerv().
54+ 		 //  NOLINTNEXTLINE(cppcoreguidelines-init-variables)
55+ 		 GLint old_fb;
56+ 		 glGetIntegerv (GL_FRAMEBUFFER_BINDING, &old_fb);
5457
55- 	glBindFramebuffer (GL_FRAMEBUFFER, this ->fbo );
56- 	assert_opengl_no_error ();
58+ 		 glBindFramebuffer (GL_FRAMEBUFFER, this ->fbo );
59+ 		 assert_opengl_no_error ();
5760
58- 	if  (this ->color ) {
59- 		ASSERT (dynamic_cast <texture_2d*>(this ->color .get ()))
60- 		//  NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
61- 		auto & tex = static_cast <texture_2d&>(*this ->color );
61+ 		 if  (this ->color ) {
62+ 			 ASSERT (dynamic_cast <texture_2d*>(this ->color .get ()))
63+ 			 //  NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
64+ 			 auto & tex = static_cast <texture_2d&>(*this ->color );
6265
63- 		glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.tex , 0 );
64- 		assert_opengl_no_error ();
65- 	} else  {
66- 		//  TODO: glDrawBuffer(GL_NONE) ? See https://gamedev.stackexchange.com/a/152047
67- 	}
66+ 			 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.tex , 0 );
67+ 			 assert_opengl_no_error ();
68+ 		 } else  {
69+ 			 //  TODO: glDrawBuffer(GL_NONE) ? See https://gamedev.stackexchange.com/a/152047
70+ 		 }
6871
69- 	if  (this ->depth ) {
70- 		ASSERT (dynamic_cast <texture_depth*>(this ->depth .get ()))
71- 		//  NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
72- 		auto & tex = static_cast <texture_depth&>(*this ->depth );
72+ 		 if  (this ->depth ) {
73+ 			 ASSERT (dynamic_cast <texture_depth*>(this ->depth .get ()))
74+ 			 //  NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
75+ 			 auto & tex = static_cast <texture_depth&>(*this ->depth );
7376
74- 		glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex.tex , 0 );
75- 		assert_opengl_no_error ();
76- 	}
77+ 			 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex.tex , 0 );
78+ 			 assert_opengl_no_error ();
79+ 		 }
7780
78- 	if  (this ->stencil ) {
79- 		throw  std::logic_error (" frame_buffer(): OpenGL stencil texture support is not implemented" 
80- 	}
81+ 		 if  (this ->stencil ) {
82+ 			 throw  std::logic_error (" frame_buffer(): OpenGL stencil texture support is not implemented" 
83+ 		 }
8184
82- 	//  check for completeness
83- 	{
84- 		GLenum status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
85- 		assert_opengl_no_error ();
86- 		if  (status != GL_FRAMEBUFFER_COMPLETE) {
87- 			throw  std::runtime_error (
88- 				utki::cat (" frame_buffer(): OpenGL framebuffer is incomplete: status = " unsigned (status))
89- 			);
85+ 		//  check for completeness
86+ 		{
87+ 			GLenum status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
88+ 			assert_opengl_no_error ();
89+ 			if  (status != GL_FRAMEBUFFER_COMPLETE) {
90+ 				throw  std::runtime_error (
91+ 					utki::cat (" frame_buffer(): OpenGL framebuffer is incomplete: status = " unsigned (status))
92+ 				);
93+ 			}
9094		}
91- 	}
9295
93- 	glBindFramebuffer (GL_FRAMEBUFFER, old_fb);
94- 	assert_opengl_no_error ();
96+ 		glBindFramebuffer (GL_FRAMEBUFFER, old_fb);
97+ 		assert_opengl_no_error ();
98+ 	});
9599}
96100
97101frame_buffer::~frame_buffer ()
0 commit comments