{"id":3647,"date":"2015-05-06T23:10:59","date_gmt":"2015-05-06T13:10:59","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=3647"},"modified":"2015-05-06T23:10:59","modified_gmt":"2015-05-06T13:10:59","slug":"opengl-framebuffer-objects-cant-be-shared-between-rendering-contexts","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2015\/05\/06\/opengl-framebuffer-objects-cant-be-shared-between-rendering-contexts\/","title":{"rendered":"OpenGL framebuffer objects can’t be shared between rendering contexts"},"content":{"rendered":"

(Title is sort of a spoiler for the content of this post)<\/p>\n

So I spent about an hour today debugging why a particular user interface element was generating OpenGL errors when I tried to draw it.\u00a0 It was this:<\/p>\n

\"soldier_croc\"<\/a><\/p>\n

This is an early revision of a character editor for MMORPG Tycoon 2.\u00a0 The interesting bit of it is the 3D character being drawn in the right side of the window.\u00a0 This is done by drawing the model into an offscreen texture, and then drawing that texture into the window.<\/p>\n

Works great here in my UI element testing mode, but broke terribly when I ran it in the real game;\u00a0 it complained that trying to set that offscreen texture as the place to draw into was completely illegal.\u00a0 But it worked when I ran the same code from the UI testing mode.<\/p>\n

So after an hour or so of investigation, the problem turned out to be simple.<\/p>\n

When I load everything for the real game, I do it from a background thread which has its own OpenGL context, which is technically a “shared” context, sharing its data with the main context.\u00a0 This is so that I can be rendering in the main context, while the background context is creating textures, models, and other OpenGL data, without worrying about trying to maintain the loading screen’s frame rate.\u00a0<\/p>\n

The gotcha is this:\u00a0 framebuffer objects (FBOs) cannot be shared between OpenGL contexts the way that vertex buffer objects (VBOs), textures, and other objects can be.\u00a0 And since I was loading the window from that background thread’s OpenGL context, and the window created the framebuffer that it was going to render, when it came time to actually draw the window on the main thread, the main thread’s OpenGL context had never heard of the FBO, and threw errors like mad.\u00a0<\/p>\n

I hadn’t seen this problem from the UI testing mode simply because that mode loads everything from the main thread’s normal OpenGL context which is used for rendering, and so that FBO which was created by the window was completely fine to render to.<\/p>\n

So there you go.\u00a0 Turns out that if you’re writing OpenGL code, shared contexts aren’t a complete panacea for multithreading issues after all; you still have to be sure to create your FBOs only from the main context, if that’s where you’re going to be rendering into them. Lesson learned.<\/p>\n

(The screenshot here is a bit old, and the models are extremely<\/em> early and placeholder.\u00a0 But this is still a whole lot nicer than the old models!\u00a0 Being able to customise the models used by your players, the monsters, and the NPCs is one of the big new features that will be present in Milestone 10.\u00a0 I’m pretty pleased at how it’s starting to shape up.)<\/p>\n","protected":false},"excerpt":{"rendered":"

(Title is sort of a spoiler for the content of this post) So I spent about an hour today debugging why a particular user interface element was generating OpenGL errors when I tried to draw it.\u00a0 It was this:<\/p>\n","protected":false},"author":1,"featured_media":3195,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[33,25],"tags":[],"jetpack_featured_media_url":"https:\/\/www.vectorstorm.com.au\/wp-content\/uploads\/2014\/05\/opengl-logo.gif","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-WP","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3647"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=3647"}],"version-history":[{"count":5,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3647\/revisions"}],"predecessor-version":[{"id":3653,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3647\/revisions\/3653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media\/3195"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=3647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=3647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=3647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}