<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mighdoll</title>
    <description>The latest articles on DEV Community by mighdoll (@mighdoll).</description>
    <link>https://dev.to/mighdoll</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F983353%2Fa6108e4c-ce40-4fbf-934b-469aaeb447a9.png</url>
      <title>DEV Community: mighdoll</title>
      <link>https://dev.to/mighdoll</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mighdoll"/>
    <language>en</language>
    <item>
      <title>Reactive WebGPU</title>
      <dc:creator>mighdoll</dc:creator>
      <pubDate>Fri, 07 Apr 2023 18:34:04 +0000</pubDate>
      <link>https://dev.to/mighdoll/reactive-webgpu-52h0</link>
      <guid>https://dev.to/mighdoll/reactive-webgpu-52h0</guid>
      <description>&lt;p&gt;WebGPU gives web developers flexible ways to use the crazy high performance parallel power of GPUs. GPU programs on the web are no longer trapped inside one canvas! &lt;/p&gt;

&lt;p&gt;But sharing WebGPU resources between multiple parts of a web application can be tricky to manage. When do you build WebGPU buffers and shaders? When do you update? When do you release old GPU resources? In a web page with shared GPU resources, resource management is complicated.&lt;/p&gt;

&lt;p&gt;One good approach is to use a fine grained reactivity library. Fine grained reactivity is helping web developers to manage the costs of updating the DOM and wrangle the complexity of update logic. Similarly, fine grained reactivity can help WebGPU developers deal with the costs and complexity of managing GPU resources.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine Grained Reactivity for HTML&lt;/li&gt;
&lt;li&gt;WebGPU Resource Management&lt;/li&gt;
&lt;li&gt;
Reactively for WebGPU

&lt;ul&gt;
&lt;li&gt;Resource Reallocation&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Dependency Tracking &lt;/li&gt;
&lt;li&gt;Lazy Recalculation&lt;/li&gt;
&lt;li&gt;Resource Cleanup&lt;/li&gt;
&lt;li&gt;Challenges&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Summary&lt;/li&gt;

&lt;li&gt;Status&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;First, a bit of background on fine grained reactivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fine Grained Reactivity for HTML
&lt;/h2&gt;

&lt;p&gt;The challenge: updating the HTML DOM is expensive, and the reasons to update are complicated. Update too often and the app is slow. Update too little and the app looks buggy. And long chains of logic to decide when to update are hard to maintain.&lt;/p&gt;

&lt;p&gt;The fine grained reactivity approach is declarative. The fine grained reactivity libraries have a lightweight way to &lt;em&gt;declare&lt;/em&gt; reactive elements, and then the libraries automatically track dependencies between reactive elements. The declarations are easy to maintain, and the app code doesn't need update logic at all. The library handles update logic by itself, by &lt;a href="https://dev.to/modderme123/super-charging-fine-grained-reactive-performance-47ph"&gt;cleverly evaluating&lt;/a&gt; the reactive graph implied by the reactive element dependencies. The 'fine grained' category of these libraries indicates that reactivity applies to JavaScript and TypeScript variables and functions. 'Fine grained' libraries differ from more 'coarse grained' libraries where reactivity only applies to larger entities like components or modules.&lt;/p&gt;

&lt;p&gt;Significant parts of the web dev community are moving to fine grained reactivity to meet the challenge of creating modern dynamic HTML. Note that fine grained reactive libraries are sometimes called Signal libraries. Libraries like &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt;, &lt;a href="https://preactjs.com/guide/v10/signals/" rel="noopener noreferrer"&gt;Preact Signals&lt;/a&gt;, &lt;a href="https://github.com/angular/angular/tree/main/packages/core/src/signals" rel="noopener noreferrer"&gt;Angular Signals&lt;/a&gt;, &lt;a href="https://www.solidjs.com/" rel="noopener noreferrer"&gt;SolidJs&lt;/a&gt;, and &lt;a href="https://vuejs.org/guide/extras/reactivity-in-depth.html" rel="noopener noreferrer"&gt;Vue Signals&lt;/a&gt; are examples of this trend.&lt;/p&gt;

&lt;p&gt;The fine grained reactive libraries provide three key features: laziness, caching, and smart recalculation. The feature set makes for good performance because they allow apps to avoid expensive DOM manipulation except when strictly necessary. And the fine grained reactive libraries make web development easier because they offload apps from most of the logic of deciding when to update. The new generation of fine grained reactivity libraries is easy to adopt too, with short APIs and only a few KB of code.&lt;/p&gt;

&lt;p&gt;Of course, lazy variables or clever recalculation schemes aren't new ideas. Functional reactive programmers and even spreadsheet users have used those ideas for many years. The ideas are not new on the web (&lt;a href="https://vuejs.org/guide/extras/reactivity-in-depth.html" rel="noopener noreferrer"&gt;vue&lt;/a&gt;, &lt;a href="https://elm-lang.org/docs/advanced-topics#functional-reactive-programming" rel="noopener noreferrer"&gt;elm&lt;/a&gt;), and the ideas have been explored in the research community (&lt;a href="https://www.flapjax-lang.org/" rel="noopener noreferrer"&gt;flapjax&lt;/a&gt;, &lt;a href="http://conal.net/papers/icfp97/" rel="noopener noreferrer"&gt;fran&lt;/a&gt;, &lt;a href="https://infoscience.epfl.ch/record/148043?ln=en" rel="noopener noreferrer"&gt;deprecating&lt;/a&gt;, &lt;a href="https://www.umut-acar.org/self-adjusting-computation" rel="noopener noreferrer"&gt;incremental&lt;/a&gt;). But fine grained reactively is becoming increasingly mainstream on the web partly due to an inspiring new generation of &lt;a href="https://dev.to/modderme123/super-charging-fine-grained-reactive-performance-47ph"&gt;lighter and faster&lt;/a&gt; implementations and partly due to the untiring explanations from advocates like &lt;a href="https://www.youtube.com/watch?v=g584AIL1HtI" rel="noopener noreferrer"&gt;Ryan&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebGPU Resource Management
&lt;/h2&gt;

&lt;p&gt;Like the browser's HTML DOM, WebGPU resources are expensive to update, and the reasons to update can be similarly complicated.&lt;/p&gt;

&lt;p&gt;WebGPU resources are very demanding in terms of CPU time and memory use. WebGPU shader programs are compiled, and compilation is relatively slow. Best not to compile them too much. WebGPU buffers and textures are often large. Best not to allocate them unnecessarily. GPU memory is limited and relatively inflexible. For example, GPUBuffers, the workhorse storage elements for WebGPU programs, don't grow and shrink automatically like JavaScript/TypeScript arrays. You need to allocate a new buffer if you need more storage. Best not to reallocate all the time though, lest your fancy parallel GPU spend all its time waiting for buffer copying.&lt;/p&gt;

&lt;p&gt;So for performance reasons, just as web developers need to avoid constantly changing the DOM, WebGPU developers need to avoid constantly building and rebuilding GPU resources.&lt;/p&gt;

&lt;p&gt;WebGPU programs and resources can naturally support multiple canvases. Imagine dashboards, simulations, interactive documents, etc. And WebGPU can easily serve as a compute engine with perhaps no graphics at all. The key flexibility is that WebGPU shaders, buffers and textures can be dynamically shared by multiple different parts of the main JavaScript/TypeScript/HTML app. Taking advantage of this flexibility leads to more complex internal interdependencies between program elements.&lt;/p&gt;

&lt;p&gt;A dynamic multipart page like a dashboard or an interactive document using WebGPU may have a complicated set of code and data dependencies that can trigger an update to the GPU resources, just like a complicated web page may have complicated reasons to update the HTML.&lt;/p&gt;

&lt;p&gt;That's the analogy between WebGPU development and web development. WebGPU resource updates are expensive and update logic is complex. It's structurally the same problem as HTML DOM updates for a web framework.&lt;/p&gt;

&lt;p&gt;Let's see how we can apply a web development style solution to WebGPU by using a fine grained reactivity library to manage WebGPU resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactively for WebGPU
&lt;/h2&gt;

&lt;p&gt;To explore fine grained reactivity for WebGPU, we'll use the &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; library by &lt;a href="https://moddermeht.ml" rel="noopener noreferrer"&gt;modderme123&lt;/a&gt;. It's fast and small and was originally designed to be independent of any particular web framework. (That said, &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; adapters for &lt;a href="https://lit.dev" rel="noopener noreferrer"&gt;Lit&lt;/a&gt; are already available, and &lt;a href="https://www.solidjs.com/" rel="noopener noreferrer"&gt;SolidJS&lt;/a&gt; is planning to adopt &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; in their 2.0 release.)&lt;/p&gt;

&lt;p&gt;The code samples below are from the Mosaic tiling plugin in the &lt;a href="https://thimbleberry.dev" rel="noopener noreferrer"&gt;Thimbleberry&lt;/a&gt; image transform demo. In the Mosaic transform, the destination image is filled with polygonal tiles where the color of each tile is sampled from the source image.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F229271188-83148124-8b5f-4aa9-994b-9ebcb2498c7e.png" class="article-body-image-wrapper"&gt;&lt;img alt="user interface showing bird picture and mosaic tile version of bird picture" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F229271188-83148124-8b5f-4aa9-994b-9ebcb2498c7e.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The user can specify the size and shape of the mosaic tiles in a control panel:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230548292-21d3f35a-aba3-4035-85bb-5769663fa28a.png" class="article-body-image-wrapper"&gt;&lt;img alt="user interface control panel" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230548292-21d3f35a-aba3-4035-85bb-5769663fa28a.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Resource Reallocation
&lt;/h3&gt;

&lt;p&gt;Let's look at how to manage the vertex buffer for the Mosaic shader with &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The vertex buffer is a suitable candidate for fine grained reactivity because the buffer will need to be updated in response to a complex set of user actions, and it's expensive to update. Our GPU will stall waiting on the CPU every time we update this buffer, so we'd rather not update the buffer too often, especially because we might be pretty busy processing video at 60 frames per second.&lt;/p&gt;

&lt;p&gt;A variety of user actions on the control panel might change the tile vertices and require that we update the vertex buffer. Our goal is to update the buffer only if one of the user actions forces a change. We could maintain a list of 'dirtying' user actions, but that sounds tedious and hard to maintain. Every time we add a new tiling feature, we'll have to take care to revise our update logic too.&lt;/p&gt;

&lt;p&gt;Let's see how fine grained reactivity manages the update problem more simply. Here's the routine that allocates the vertex buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;reactively&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;vertexBuffer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;GPUBuffer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;verts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shapeVerts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GPUBufferUsage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VERTEX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;filledGPUBuffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;verts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mosaic-verts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;reactiveTrackUse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usageContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key trick is simply adding &lt;code&gt;@reactively&lt;/code&gt; annotations. The annotation turns on the fine grained reactivity features of dependency tracking, caching, and lazy recalculation for this method. &lt;/p&gt;

&lt;h3&gt;
  
  
  Caching
&lt;/h3&gt;

&lt;p&gt;While processing a video stream, the &lt;code&gt;GPUBuffer&lt;/code&gt; will be needed on every frame and so the &lt;code&gt;vertexBuffer&lt;/code&gt; getter will be called on every frame. That sounds expensive. But we're ok. The &lt;code&gt;@reactively&lt;/code&gt; annotation gives us caching automatically, so the getter body will only be called on the first frame. Subsequent calls will return a cached value without executing the code in the method body.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency Tracking
&lt;/h3&gt;

&lt;p&gt;But what if the vertices change? What if the user changes the shape and number of the tiles while the video is playing? In that case, we will need to update the buffer. &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; can handle that situation automatically. &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; tracks dependencies and will re-execute the body of the getter if the dependencies change, &lt;code&gt;vertexBuffer&lt;/code&gt; will create a new &lt;code&gt;GPUBuffer&lt;/code&gt; as required. In this case, &lt;code&gt;shapeVerts&lt;/code&gt; might change if the user changes the size and shape of the mosaic tiles in the control panel and then it would be appropriate to rebuild the GPUBuffer for new vertices for the next rendered frame.&lt;/p&gt;

&lt;p&gt;So that &lt;code&gt;shapeVerts&lt;/code&gt; is reactively tracked, we mark it &lt;code&gt;@reactively&lt;/code&gt; too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;reactively&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;shapeVerts&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;destSize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;xt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;yt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mosaicSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;tile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;tile&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scaledVertsNDC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rawVerts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;xt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;yt&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;scaledVertsNDC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we similarly annotate &lt;code&gt;rawVerts&lt;/code&gt;, &lt;code&gt;mosaicSize&lt;/code&gt;, and &lt;code&gt;destSize&lt;/code&gt; with &lt;code&gt;@reactively&lt;/code&gt;. For example, here we annotate the &lt;code&gt;mosaicSize&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;reactively&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;deepEqual&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="nx"&gt;mosaicSize&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vec2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;deepEqual&lt;/code&gt; so that reactive checking uses &lt;code&gt;mosaicSize&lt;/code&gt; array contents, rather than the array identity. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; automatically detects that &lt;code&gt;shapeVerts&lt;/code&gt; depends on &lt;code&gt;mosaicSize&lt;/code&gt; and that &lt;code&gt;vertexBuffer&lt;/code&gt; depends on &lt;code&gt;shapeVerts&lt;/code&gt;. That is, just from the &lt;code&gt;@reactively&lt;/code&gt; annotations, &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; detects the dependency relationships:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230543686-469f61a5-0b61-4d2a-b1df-8b8318250fdb.png" class="article-body-image-wrapper"&gt;&lt;img alt="flowchart showing connections between the reactive variables" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230543686-469f61a5-0b61-4d2a-b1df-8b8318250fdb.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Lazy Recalculation
&lt;/h3&gt;

&lt;p&gt;When the user changes the tile size in the control panel, the event handler modifies &lt;code&gt;mosaicSize&lt;/code&gt;. When the application next asks for &lt;code&gt;vertexBuffer&lt;/code&gt;, &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; will automatically re-execute the function bodies of &lt;code&gt;shapeVerts&lt;/code&gt; and &lt;code&gt;vertexBuffer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230543818-0b75c946-db7e-4fd8-9b9b-d0460778d19b.png" class="article-body-image-wrapper"&gt;&lt;img alt="flowchart with reactive variables, highlighting dirty chain from mosaicSize to shapeVerts to vertexBuffer" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230543818-0b75c946-db7e-4fd8-9b9b-d0460778d19b.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Resource Cleanup
&lt;/h3&gt;

&lt;p&gt;After we create a new buffer for newly changed vertices, what happens to the old GPUBuffer? We could hope that garbage collection will clean up, and it will, eventually. But GPU resources can be large, and garbage collection is uncertain, so WebGPU provides a &lt;code&gt;destroy()&lt;/code&gt; method on &lt;code&gt;GPUBuffer&lt;/code&gt; and similar resources to reclaim their memory without delay. Let's make sure we call &lt;code&gt;destroy()&lt;/code&gt; on unneeded vertex buffers. The fine grained reactivity libraries all include some variation of a cleanup API that we can leverage to handle &lt;code&gt;destroy&lt;/code&gt; in WebGPU.&lt;/p&gt;

&lt;p&gt;The key line in the &lt;code&gt;vertexBuffer()&lt;/code&gt; body above is this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reactiveTrackUse(buffer, this.usageContext);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reactiveTrackUse&lt;/code&gt; uses &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt;'s &lt;code&gt;onCleanup()&lt;/code&gt; method to register a callback when the getter is re-run. That's a perfect time to release the old buffer. And in fact, &lt;code&gt;reactiveTrackUse&lt;/code&gt; typically will destroy() the old buffer immediately. Under the hood, &lt;code&gt;reactiveTrackUse&lt;/code&gt; tries to be a little more clever. It uses &lt;a href="https://github.com/mighdoll/thimbleberry" rel="noopener noreferrer"&gt;Thimbleberry&lt;/a&gt;'s &lt;code&gt;trackUse&lt;/code&gt; reference counting utility which will defer calling &lt;code&gt;destroy()&lt;/code&gt; in the case where the buffer is still in some other part of our application.&lt;/p&gt;

&lt;p&gt;Here's &lt;code&gt;reactiveTrackUse&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** (for use within reactively reaction).
 * Track a destroyable resource and release the resource if the reaction reruns */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reactiveTrackUse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HasDestroy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TrackContext&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;trackUse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;onCleanup&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;trackRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Challenges When Using Reactivity for WebGPU
&lt;/h3&gt;

&lt;p&gt;Relying on laziness and dependency tracking takes a different perspective, but we've really made very few code changes, mostly just adding a few annotations to reap significant benefits. While it's a win overall, using &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; for WebGPU brings some challenges.&lt;/p&gt;

&lt;p&gt;First, reactivity tends to be contagious. Because reactive functions only track dependencies with other reactive elements, it quickly becomes tempting to make more and more things reactive. Web programmers who use &lt;code&gt;Promise&lt;/code&gt;s will be familiar with the similar 'function coloring' problem of mixing async and synchronous code. Reactivity is quite lightweight, so there's not much cost in adding a few more reactive elements. But when adding reactivity to an existing code base, it's worthwhile to think a bit about where the boundaries should lie.&lt;/p&gt;

&lt;p&gt;Second, there's little compile time support for checking that reactivity is correct. If you forget to mark something as reactive, no type error or lint rule is likely to warn you. Of course, there's no compile time support if we try to write update logic with traditional imperative code. But the possibility of linting or type checking is clear when reactivity is declarative. &lt;a href="https://react.dev/learn/lifecycle-of-reactive-effects#react-verifies-that-you-specified-every-reactive-value-as-a-dependency" rel="noopener noreferrer"&gt;React&lt;/a&gt;'s linter, for example, provides warnings for their coarse grained reactivity system. I expect that compile time support for fine grained reactivity tools will improve as web frameworks with compilation and lint tools develop further.&lt;/p&gt;

&lt;p&gt;Finally, lazy execution means giving up some control of exactly when your code runs. As with using a web framework, that can make debugging a bit harder. Execution in these small fine grained reactivity libraries isn't that complicated, but I expect to see debugging tools appear to help identify cases of missing dependencies or unexpected cache misses.&lt;/p&gt;

&lt;p&gt;Note that while the example code in this post uses decorators and Typescript classes, &lt;a href="https://github.com/modderme123/reactively" rel="noopener noreferrer"&gt;Reactively&lt;/a&gt; works equally well with a more functional style.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;With fine grained reactively, GPU resources are rebuilt when necessary and no more. Caching, dependency tracking, and smart recalculation come more or less for free. We don't need to write and maintain separate logic for dirty checking, and eager resource destruction is easier to manage.&lt;/p&gt;

&lt;p&gt;Here's a more complete look at the dependencies in the Mosaic shader:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230544196-a0b69a85-8c67-47e7-b563-32d08d601d91.png" class="article-body-image-wrapper"&gt;&lt;img alt="image" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F63816%2F230544196-a0b69a85-8c67-47e7-b563-32d08d601d91.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We could work out the logic to update the GPU resources and write the update code manually, but it's easier to just add a few annotations and let the library handle it.&lt;/p&gt;

&lt;p&gt;The fine grained reactivity libraries built for general web development turn out to be very useful for WebGPU too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Status
&lt;/h3&gt;

&lt;p&gt;WebGPU is available in preview releases of Chrome and Firefox today. General release in Chromium based browsers is coming by summer, with Firefox soon to follow. You can try mixing WebGPU and reactivity right now if you use Chrome Canary. The demo site using this example Mosaic shader is available here: &lt;a href="https://thimbleberry.dev" rel="noopener noreferrer"&gt;Thimbleberry&lt;/a&gt; (&lt;a href="https://github.com/mighdoll/thimbleberry" rel="noopener noreferrer"&gt;src&lt;/a&gt;).&lt;/p&gt;

</description>
      <category>angular</category>
      <category>solidjs</category>
      <category>webgpu</category>
    </item>
  </channel>
</rss>
