{"id":116,"date":"2022-08-30T12:56:55","date_gmt":"2022-08-30T12:56:55","guid":{"rendered":"https:\/\/leonwoud.com\/devblog\/?p=116"},"modified":"2022-09-13T12:52:13","modified_gmt":"2022-09-13T12:52:13","slug":"yo-dawg-i-heard-you-liked-refactoring","status":"publish","type":"post","link":"https:\/\/leonwoud.com\/devblog\/yo-dawg-i-heard-you-liked-refactoring\/","title":{"rendered":"Yo dawg, I heard you liked refactoring&#8230;"},"content":{"rendered":"\n<figure class=\"wp-block-video\"><video height=\"700\" style=\"aspect-ratio: 852 \/ 700;\" width=\"852\" controls src=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/t-roy.mp4\"><\/video><\/figure>\n\n\n\n<p>You know that thing you do when you&#8217;re learning something new, so you make every mistake under the sun and \/ or massively over complicate \/ engineer things? Yeah, I did that.<\/p>\n\n\n\n<p>As I mentioned in my <a href=\"https:\/\/leonwoud.com\/devblog\/weve-got-textures-everybody\/\">last post<\/a>, I did get textures working &#8211; but there was a large caveat. I only had  a single shared descriptor set, which meant all meshes shared the same textures.<\/p>\n\n\n\n<p>So I did a bit of refactoring, I moved the descriptor set creation to the model component of the Game Object so my render system could grab them. I then decided I wanted a UBO for certain shared properties for debugging purposes (I already had a &#8220;GlobalUBO&#8221; for things like the cameras projection matrix etc.). So I moved the descriptor set creation to the render system itself, which meant I then had to cache the these descriptor sets in the render system. This didn&#8217;t feel right, even at the time, but I went and forged ahead anyway.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"147\" height=\"160\" src=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/image.png\" alt=\"\" class=\"wp-image-119\"\/><\/figure>\n\n\n\n<p>This was obviously the wrong decision, and for a number of reasons.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Having to cache in the render system means a cached descriptor set per Game Object, so if I have multiple instances of the same Game Object (like my 3 T-Roys above), You have multiple copies of the same descriptor sets that could and should be shared. This is a solvable problem, you could just cache a bit cleverer, but it is extra work unnecessary work.<\/li><li>You have to keep track of this cache when Game Objects are destroyed so you can free the descriptor sets.<\/li><li>The properties I needed to pass are actually per model, not shared for all Game Objects and the shared properties made sense to go into the GlobalUBO. I don&#8217;t know what I was thinking honestly.<\/li><\/ul>\n\n\n\n<p>Moving the descriptor set creation back to the model class makes the most sense I think, it means there is no need for a cache, the descriptor sets will be per model and they can be freed when there is no longer any references to the model (they&#8217;re a shared pointer on a Game Object currently). It does mean that the model class is tied explicitly to the render system (or at least the pipeline layout of the render system), but I can&#8217;t see why that would be a problem (until I have more render systems that use the same models? I&#8217;ll cross that bridge&#8230;)<\/p>\n\n\n\n<p>So yeah, more refactoring needed&#8230; I think it would pay to plot out the engine architecture while it&#8217;s still nice and simple. That way I could potentially see the impact of my bad decisions before I make them&#8230; maybe?<\/p>\n\n\n\n<p>On a more positive note, I did get a couple of debug drawing modes hooked up to hotkeys which is nice, when I have a text overlay system implemented I&#8217;ll be able to see what kind of impact my shaders are having on the FPS and other fun stuff.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"806\" src=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-1024x806.png\" alt=\"\" class=\"wp-image-120\" srcset=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-1024x806.png 1024w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-300x236.png 300w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-768x604.png 768w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-1536x1209.png 1536w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-250x197.png 250w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-550x433.png 550w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-800x630.png 800w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-229x180.png 229w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-381x300.png 381w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM-635x500.png 635w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.03.30-AM.png 1606w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Debug Mode: No Lighting<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"812\" src=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-1024x812.png\" alt=\"\" class=\"wp-image-121\" srcset=\"https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-1024x812.png 1024w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-300x238.png 300w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-768x609.png 768w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-1536x1218.png 1536w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-250x198.png 250w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-550x436.png 550w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-800x635.png 800w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-227x180.png 227w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-378x300.png 378w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM-630x500.png 630w, https:\/\/leonwoud.com\/devblog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-31-at-12.04.39-AM.png 1606w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Debug Mode: No textures<\/figcaption><\/figure>\n\n\n\n<p>I have finished the Vulkan Cookbook, I suspect I&#8217;ll go back to it here and there when needed.<\/p>\n\n\n\n<p>Oh and lastly, I learnt about memory alignment after banging my head against a wall for a while trying to figure out why a couple of bools I was trying to pass through push constants \/ uniform buffer objects were spontaneously changing state.<\/p>\n\n\n\n<p>Anyway, back to refactoring.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You know that thing you do when you&#8217;re learning something new, so you make every mistake under the sun and \/ or massively over complicate \/ engineer things? Yeah, I did that. As I mentioned in my last post, I did get textures working &#8211; but there was a large [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[9],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-game-dev"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":4,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/posts\/116\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leonwoud.com\/devblog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}