<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Evolve Blog</title><link>https://www.evolvebenchmark.com</link><description>Evolve Benchmark articles</description><item><title>3D Gaussian Splatting - Rendering Gaussian Splats in real-time</title><link>https://www.evolvebenchmark.com/blog-posts/3d-gaussian-splatting-rendering-gaussian-splats-in-real-time</link><guid>https://www.evolvebenchmark.com/blog-posts/3d-gaussian-splatting-rendering-gaussian-splats-in-real-time</guid><description>&lt;p&gt;Hello, my name is Konstantin, and I am currently in my second year at Breda University of Applied Sciences. This summer, I had the exciting opportunity to complete an internship at Traverse Research, where my primary task was to develop a real-time Gaussian Splatting viewer. In this blog post, I will share my approach to implementing the visualization part of the Gaussian Splatting paper using the Breda framework at Traverse Research.&lt;/p&gt;
&lt;p&gt;In recent years, 3D scanning and rendering have been the focus of significant research, leading to innovative developments. One such breakthrough in this field is the development of Neural Radiance Field methods, which have revolutionized novel-view synthesis in computer graphics by producing highly detailed and photorealistic renderings.&lt;/p&gt;
&lt;p&gt;Traditional photogrammetry often struggles with varying lighting conditions and capturing fine details. NeRF models attempt to alleviate these issues by utilizing a neural network to represent the scene’s radiance and density. The process involves training the neural network (the radiance field) to match a set of input 2D images by sampling the network’s output through ray marching and minimizing the difference between the input views and the rendered images.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1655pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-1.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Source: The &lt;a href=&quot;https://www.matthewtancik.com/nerf&quot; target=&quot;_blank&quot;&gt;NeRF paper&lt;/a&gt;, fig. 1&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;However, NeRF is computationally demanding, as the process of training a NeRF model and rendering views is resource-intensive, often requiring significant processing time and power.&lt;/p&gt;
&lt;p&gt;3D Gaussian Splatting, originally introduced in the paper &lt;a href=&quot;https://repo-sam.inria.fr/fungraph/3d-gaussian-splatting/&quot;&gt;&lt;em&gt;3D Gaussian Splatting for Real-Time Radiance Field Rendering&lt;/em&gt;&lt;/a&gt;, tries to address these issues by using 3D Gaussians to represent the scene. This method approximates volume rendering by projecting (or “splatting”) the Gaussians and blending the results, rather than marching rays through the scene. In contrast to Neural Radiance Fields, this approach optimizes a set of Gaussians rather than a neural network and utilizes rasterization, allowing for significantly faster rendering compared to ray marching. However, compared to NeRF, the storage requirements are significantly higher, which still poses challenges for practical deployment.&lt;/p&gt;
&lt;h2 id=&quot;representing-a-scene-with-gaussians&quot;&gt;Representing a scene with Gaussians&lt;/h2&gt;
&lt;p&gt;In Gaussian Splatting, a 3D scene is represented by a set of points, each corresponding to a 3D Gaussian. Unlike NeRF, where the scene is modeled using a neural network, Gaussian Splatting represents the scene directly with these 3D Gaussians.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1655px&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-2.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A scene represented with only points in space, captured from Breda framework&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Each Gaussian is fitted with a unique set of parameters so that the rendered images match the known dataset. The parameters of each Gaussian include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Mean (or position), which defines the center of the 3D Gaussian&lt;/li&gt;
&lt;li&gt;Covariance matrix, which determines scale and rotation of the Gaussian&lt;/li&gt;
&lt;li&gt;Opacity&lt;/li&gt;
&lt;li&gt;Color ,  either RGB values or spherical harmonics coefficients&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1000px&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-3.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Individual ellipsoids, captured from Breda framework&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;implementing-the-3d-gaussian-splatting-rendering-algorithm&quot;&gt;Implementing the 3D Gaussian Splatting Rendering Algorithm&lt;/h2&gt;
&lt;p&gt;Now that we understand Gaussians and how to represent a scene with them, we can delve into the practical implementation of the rendering algorithm suggested in the original paper. Although it is possible to use the standard rasterization pipeline for rendering Gaussians, using compute shaders with tile-based rasterization offers finer control over GPU resources used and can enhance performance through fast GPU sorting algorithms for alpha blending.&lt;/p&gt;
&lt;p&gt;For context, in this example, I’ll use the Breda framework, which supports render graphs, bindless resources, and HLSL shader code (compiled for DX12, Vulkan and Metal backends). However, the logic and structure of this rendering algorithm can be adapted to any modern rendering API.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method Overview:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Split the Screen into Tiles:&lt;/strong&gt; Divide the screen into a grid of tiles (e.g., 16x16 pixels).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pre-process Gaussians:&lt;/strong&gt; Transform Gaussians into screen space, calculate how many tiles each Gaussian occupies, and compute their color and depth from the camera’s  point of view.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Create Keys for Each Gaussian Instance:&lt;/strong&gt; Duplicate Gaussians that occupy more than one tile and assign keys (containing tile ID and depth) to each instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sort Keys:&lt;/strong&gt; Sort the keys based on the tile ID and the depth values of the Gaussians.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Calculate Tile Workload Ranges:&lt;/strong&gt; Determine the workload for each tile based on sorted keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Render Tiles:&lt;/strong&gt; Use the sorted data to render each tile efficiently.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Since this blog post focuses on the rendering part, we can use pre-trained models provided by the original paper’s authors. These models, stored as &lt;code&gt;.ply&lt;/code&gt; files, include all parameters for each Gaussian. After extracting the necessary data from the file, we start rendering by first splitting the screen into tiles. For now this part doesn’t require any code, so we can proceed to the pre-processing step to prepare Gaussians for rendering.&lt;/p&gt;
&lt;h2 id=&quot;pre-processing-step&quot;&gt;Pre-processing Step&lt;/h2&gt;
&lt;p&gt;The pre-processing step involves transforming Gaussians into screen space, determining the number of tiles each Gaussian occupies, and computing the output color using spherical harmonics.&lt;/p&gt;
&lt;p&gt;We start by creating a set of buffers from the data obtained from the &lt;code&gt;.ply&lt;/code&gt; file, storing Gaussian parameters: positions, opacities, and spherical harmonics coefficients. If we choose to compute the 3D covariance matrix on the GPU, we store scale and rotation values; alternatively, we can precompute this matrix on the CPU and only send the upper half to the GPU, as the matrix is symmetric. This pre-processed data forms our input.&lt;/p&gt;
&lt;p&gt;For the output, we store:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Screen Space Position:&lt;/strong&gt; The position of the Gaussian in screen space.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Depth:&lt;/strong&gt; Depth information for each Gaussian.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Radius:&lt;/strong&gt; The projected 2D radius of each Gaussian.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RGB Color:&lt;/strong&gt; Calculated from spherical harmonics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conic Section:&lt;/strong&gt; Stores a 3D vector defining the splatted Gaussian; we can additionally pack it with the original opacity obtained from the file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tile Count:&lt;/strong&gt; Stores how many tiles each Gaussian occupies.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Within the Breda framework, creating these buffers and using the compute shader pipeline is especially straightforward. The framework’s abstractions remove the need for users to have specific knowledge of the underlying rendering APIs, ensuring compatibility across Vulkan, DX12 and Metal, allowing to focus on developing the algorithms and logic.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Create static buffers for pre-process input
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; positions &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; render_graph_persistent_store.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;create_static_buffer_with_data&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;	&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;gaussian positions&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;	&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BufferCreateDesc&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;gpu_only_constants&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;	&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_data.positions&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Create storage buffers for pre-process output
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; point_image_buffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; render_graph.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;create_storage_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;10&lt;/span&gt;	&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;point image out buffer&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;11&lt;/span&gt;	gaussian_bindings.num_of_gaussians &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;std&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;mem&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;size_of&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;12&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Pre-processing the gaussians
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;15&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Involves transforming them into screen space,
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;16&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; determining the number of tiles each Gaussian occupies and
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;17&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; computing the output color from spherical harmonics
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;18&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ComputePass&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Preprocess Gaussians&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; render_graph&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;19&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;viewer_constants&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;20&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.positions&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;21&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.scales&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;22&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.opacities&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;23&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.rotations&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;24&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.cov_3d&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;25&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;gaussian_bindings.sh&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;26&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;point_image_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;27&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;depth_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;28&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;radii_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;29&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;rgb_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;30&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;conic_opacity_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;31&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;tiles_buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;32&lt;/span&gt;    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;33&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;shader_db.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;get_pipeline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;preprocess-gaussians&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;34&lt;/span&gt;        gaussian_bindings.num_of_gaussians.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;div_ceil&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;GROUP_SIZE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;35&lt;/span&gt;        &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;36&lt;/span&gt;        &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;37&lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;In the pre-processing shader, we first check if the mean (position) of the Gaussian is within the view frustum. If it is, we proceed to compute the covariance in 2D, from which we can derive the &lt;a href=&quot;https://en.wikipedia.org/wiki/Conic_section&quot;&gt;conic section&lt;/a&gt; and radius of the Gaussian:&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;float3 cov &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; computeCov2D&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;mean&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; focalX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; focalY&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; 
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;                          tanFovX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; tanFovY&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; cov3D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; worldToView&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;float det &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;cov.x &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; cov.z &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; cov.y &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; cov.y&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;det &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;float det_inv &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1.&lt;/span&gt;f &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;/&lt;/span&gt; det&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;float3 conic &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;cov.z &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; det_inv&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt;cov.y &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; det_inv&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; cov.x &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; det_inv&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;11&lt;/span&gt;float mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;5&lt;/span&gt;f &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;cov.x &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt; cov.z&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;12&lt;/span&gt;float lambda1 &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1&lt;/span&gt;f&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; det&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;13&lt;/span&gt;float lambda2 &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1&lt;/span&gt;f&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; mid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; det&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;14&lt;/span&gt;float radius &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;ceil&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;3.&lt;/span&gt;f &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;lambda1&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; lambda2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;The computation of the covariance matrix in 2D follows the steps outlined in equations 29 and 31 of the “EWA Splatting” method (Zwicker et al., 2002), with modifications to account for screen size and aspect ratio (as  used in the the original paper).&lt;/p&gt;
&lt;p&gt;Calculating the screen space coordinates and depth is straightforward. However, we still need to determine how many screen-space tiles each Gaussian occupies. To do this, we compute the area of the axis-aligned bounding box (AABB) that the projected Gaussian covers in 2D. Knowing the area of the tiles allows us to calculate the number of tiles a Gaussian occupies.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;uint2 tileGrid &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;uint2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;resolution&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockX&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockX&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;                      &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;resolution&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockY&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockY&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getRectFromRadius&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pointImage&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; radius&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; rectMax&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; tileGrid&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; tilesTouched &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;rectMax&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;rectMax&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Quit if processed Gaussian occupies 0 tiles
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;tilesTouched &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;The final task in the pre-processing step is to compute the color. This can be done using either RGB values or spherical harmonics. Spherical harmonics (often referred to as SH) are particularly useful for encoding colors in a view-dependent manner, which improves the quality of renders by  allowing the model to represent non-Lambertian effects, such as the specularity of metallic surfaces.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;key-generation-and-sorting&quot;&gt;Key Generation and Sorting&lt;/h2&gt;
&lt;p&gt;Next, we compute the inclusive sum of the buffer containing the number of occupied tiles. For example, if the input buffer is &lt;code&gt;[0, 2, 0, 1, 3]&lt;/code&gt;, the output buffer would be &lt;code&gt;[0, 2, 2, 3, 6]&lt;/code&gt;, representing the cumulative sum. To achieve this, we implement a parallel prefix sum algorithm. While &lt;a href=&quot;https://discussions.unity.com/t/parallel-prefix-sum-computeshader/692358/15&quot;&gt;this&lt;/a&gt; implementation, sourced from Unity forums, may not be the most efficient, it is well-suited for Gaussian Splatting and compatible with HLSL code. The resulting output buffer provides the offsets necessary for generating keys and determining the total number of Gaussian instances to render.&lt;/p&gt;
&lt;p&gt;With the number of Gaussians to render known, we allocate buffers for keys and values. The paper suggests using a 64-bit value for keys, combining both depth and tile ID, which allows efficient sorting. During key generation, we iterate through each tile occupied by a Gaussian, writing entries into the key and value buffers based on the offsets from the previous step.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; threadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_DispatchThreadID&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;    BufferBindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;loadBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;BufferBindings&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; threadId&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;gaussianNum&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;    uint3 tileGrid &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;uint3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;resolution&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockX&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockX&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;                           &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;resolution&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockY&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBlockY&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;10&lt;/span&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; radius &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;radii&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;idx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;12&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;radius &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;13&lt;/span&gt;        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; offset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;offsets&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;14&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;15&lt;/span&gt;        uint2 rectMin&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; rectMax&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;16&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;17&lt;/span&gt;        float2 xy &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;pointImage&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;float2&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;idx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;18&lt;/span&gt;        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; depth &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;idx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;20&lt;/span&gt;        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getRectFromRadius&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;xy&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; radius&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; rectMax&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; tileGrid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;21&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;22&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; y &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; y &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; rectMax&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; y&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;23&lt;/span&gt;            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; x &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; rectMin&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; x &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; rectMax&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; x&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;24&lt;/span&gt;                DuplicateKey key &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;DuplicateKey&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;25&lt;/span&gt;                key&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;tileId&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; tileGrid&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;26&lt;/span&gt;                key&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; depth&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;27&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;28&lt;/span&gt;                bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;gaussianKeys&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;DuplicateKey&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;offset&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; key&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;29&lt;/span&gt;                bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;gaussianValues&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;offset&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; idx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;30&lt;/span&gt;                offset&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;31&lt;/span&gt;            &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;32&lt;/span&gt;        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;33&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;Once keys are generated, we sort them. The Breda framework provides GPU sorting algorithms such as FFX and OneSweep, which were straightforward to use.&lt;/p&gt;
&lt;h2 id=&quot;tile-workload-calculation&quot;&gt;Tile Workload Calculation&lt;/h2&gt;
&lt;p&gt;With a sorted list of Gaussian keys and values, we compute the workload for each tile. This step eliminates the need to check for workloads in the rendering shader. By checking where in the sorted list of keys tile ID changes, and by storing thread ID, we create a buffer that defines workload ranges for each tile for rendering.&lt;/p&gt;
&lt;h2 id=&quot;rasterization&quot;&gt;Rasterization&lt;/h2&gt;
&lt;p&gt;For rasterization, we launch one thread block per 16x16 pixel tile. Each dispatch processes one tile by loading packets of Gaussians into shared memory. For each pixel in the tile, we accumulate color and alpha (α) values (blending is based on the initial sorting). When the target saturation α is reached in a pixel, the corresponding thread stops, and the output is written into the render target.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;div class=&quot;w-embed w-iframe&quot;&gt;&lt;div style=&quot;position: relative; padding-top: 56.25%;&quot;&gt;
&lt;iframe allow=&quot;accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;&quot; allowfullscreen=&quot;true&quot; loading=&quot;lazy&quot; src=&quot;https://customer-pszn90imux6s9phc.cloudflarestream.com/6c5170d22c356c31b2c8f8a5811ece06/iframe?muted=true&amp;amp;autoplay=true&amp;amp;poster=https%3A%2F%2Fcustomer-pszn90imux6s9phc.cloudflarestream.com%2F6c5170d22c356c31b2c8f8a5811ece06%2Fthumbnails%2Fthumbnail.jpg%3Ftime%3D%26height%3D600&quot; style=&quot;border: none; position: absolute; top: 0; left: 0; height: 100%; width: 100%;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;There are still a few opportunities to improve Gaussian Splats rendering, such as optimizing the size of the structures used, explored by &lt;a href=&quot;https://aras-p.info/&quot;&gt;Aras Pranckevičius&lt;/a&gt; in his &lt;a href=&quot;https://aras-p.info/blog/2023/09/27/Making-Gaussian-Splats-more-smaller/&quot;&gt;blog post&lt;/a&gt;, or &lt;a href=&quot;https://github.com/graphdeco-inria/gaussian-splatting/issues/294#issuecomment-1772688093&quot;&gt;issues&lt;/a&gt; related to approximation in both training and rendering, addressed in the &lt;a href=&quot;https://niujinshuchong.github.io/mip-splatting/&quot;&gt;following research&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As an intern, I gained valuable experience and enjoyed working with a talented and supportive team at Traverse Research. The environment in the company made it easy to seek help when needed. The codebase, with its efficient implementation and clear abstractions of common algorithms like sorting, was a pleasure to work with.&lt;/p&gt;
</description></item><item><title>Bindless rendering: Setup</title><link>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-setup</link><guid>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-setup</guid><description>&lt;p&gt;This is the first of a three-part series on writing a modern bindless rendering pipeline.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-templates-8920ca41326f&quot;&gt;Part 2: Bindless rendering: templates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-validation-ffcae4fdbe1e&quot;&gt;Part 3: Bindless rendering: validation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We will mainly cover the basics of why and how to set up a bindless pipeline in D3D12 and Vulkan. This includes setting up a bindless descriptor heap, root signatures/pipeline layouts, descriptor management, and communicating descriptor indices to the GPU. Rust will be used for code examples.&lt;/p&gt;
&lt;p&gt;In part 2 of this series we will cover a modern approach of using bindless on the GPU using the recently added template support. This will also cover how to emulate &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; functionality while running Vulkan until it’s officially supported.&lt;/p&gt;
&lt;p&gt;Finally in part 3 we will cover a lot of necessary bindless GPU validation. This includes resource versioning, resource tagging, and checking for appropriate resource creation flags.&lt;/p&gt;
&lt;h2 id=&quot;why-bindless&quot;&gt;Why bindless?&lt;/h2&gt;
&lt;p&gt;When traditionally binding resources for each draw call using descriptor staging, setup and management code tends to get relatively complex. Besides code complexity, performance also plays an important part. With the old &lt;code&gt;bindful&lt;/code&gt; model, you would first need to stage descriptors from a CPU descriptor heap to a smaller GPU descriptor heap, then set your root signature, and finally bind the correct pipeline state. All of these calls have a bit of overhead, some more than others, but more importantly, they require much more information about how a pipeline is structured, which inevitably leads to complex code. With bindless, all pipelines share the same descriptor heap, and pipeline resource layout.&lt;/p&gt;
&lt;p&gt;Besides the reduced need for pipeline-specific information, it also becomes a lot more intuitive for the user, as the concept of indexing into a piece of memory that describes a resource makes a lot more sense, compared to having several indirections and unnecessary restrictions with the old &lt;code&gt;bindful&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;To summarize, we only need to worry about a single piece of memory that points to our resources. Another benefit is that all pipelines share the same layout, which results in a simplified rendering pipeline.&lt;/p&gt;
&lt;p&gt;Bindless rendering does not only consist of benefits however, there are several constraints introduced with the technique, which I will cover throughout this series.&lt;/p&gt;
&lt;h2 id=&quot;render-resource-handles&quot;&gt;Render resource handles&lt;/h2&gt;
&lt;p&gt;With the bindless technique that we’re going to cover in this post, a &lt;em&gt;RenderResourceHandle&lt;/em&gt; is introduced that will be used as an API-agnostic descriptor handle. This handle maps directly to an index within the bindless heap, along with containing various additional validation information. These handles are &lt;strong&gt;exclusively&lt;/strong&gt; created during resource creation.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;Copy&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Clone&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Eq&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; PartialEq&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Hash&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;transparent&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-struct syntax-rust&quot;&gt;struct&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-rust&quot;&gt;RenderResourceHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the recently added &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; in SM 6.6, we can now easily index resources using a simple uint. In reality, it is very unlikely that we need to use all of the 32 bits to index the descriptor heap, this allows us to do more than just indexing descriptors, such as validating the &lt;em&gt;RenderResourceHandle&lt;/em&gt; version, validating resource types, and finally validating the resource access type. Bindless GPU validation will be covered in part 3 of this series, so for now we will assume a simple uint as an index.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-impl syntax-rust&quot;&gt;impl&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-impl syntax-rust&quot;&gt;RenderResourceHandle&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;_version&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u8&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;_tag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceTag, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;_access_type&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; AccessType&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;d3d12-setup&quot;&gt;D3D12 setup&lt;/h2&gt;
&lt;h3 id=&quot;descriptor-heap-setup&quot;&gt;Descriptor heap setup&lt;/h3&gt;
&lt;p&gt;D3D12 separates descriptor heaps into four categories; &lt;em&gt;RTV, DSV, CBV_SRV_UAV&lt;/em&gt; and &lt;em&gt;SAMPLER&lt;/em&gt;. Out of these heaps, there are only two heap types that we can use in our shaders; &lt;em&gt;CBV_SRV_UAV&lt;/em&gt; and &lt;em&gt;SAMPLER&lt;/em&gt; heaps. In our case with an API-agnostic render pipeline design in mind, we decided that we want to keep as much support available across devices and use static samplers instead of creating a bindless sampler setup (VK in particular has some constraints regarding device limits).&lt;/p&gt;
&lt;p&gt;This leaves us with having to set up a bindless system for &lt;em&gt;CBV_SRV_UAV&lt;/em&gt; descriptor types which will be used to index resources in shaders, however, if a bindless sampler approach for a more specialized renderer is needed, the exact same technique can be used as well.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;CBV, SRV,&lt;/em&gt; and &lt;em&gt;UAV&lt;/em&gt; descriptors share the same descriptor heap within the D3D12 API, this makes it especially convenient to manage descriptors; all resource descriptors also share the same descriptor size -thus the descriptors can be directly indexed within a single GPU visible descriptor heap. With the introduction of &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; in SM6.6, this heap can be directly indexed from the GPU without having to set up descriptor ranges.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;-Note that actual descriptor sizes may vary for different descriptor types, but these are rounded up to a constant size for each descriptor in the CBV_SRV_UAV heap within the D3D12 API.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now that we have a way to dynamically index our descriptor heap for resource descriptors, we can set up our actual descriptor heaps. Most modern GPU’s should support large descriptor heaps (1,000,000+). You can check the d3d12 &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/direct3d12/hardware-support&quot;&gt;hardware tiers&lt;/a&gt; to ensure the descriptor heaps are created according to the hardware tier of the device that the application runs on.&lt;/p&gt;
&lt;p&gt;For the bindless heap, we want to make sure that we are creating a &lt;em&gt;CBV_SRV_UAV&lt;/em&gt; heap that is &lt;em&gt;shader-visible&lt;/em&gt;, with bindless we don’t need descriptor staging anymore.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Ensure the descriptor_count is below the device supported number before creating the heap
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; heap_desc &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_HEAP_DESC&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    Type&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    NumDescriptors&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    Flags&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    NodeMask&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only working with a shader visible heap adds constraints for some niche d3d12 calls that require &lt;em&gt;non-shader-visible&lt;/em&gt; CPU handles, for example, &lt;em&gt;ClearUnorderedAccessViewUint.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;root-signature-setup&quot;&gt;Root signature setup&lt;/h3&gt;
&lt;p&gt;With the concept of using a single GPU visible descriptor heap to index our resources, a root signature needs to be set up in such a way that all the shaders can index the bindless descriptor heap. There are two ways to approach this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The pre-SM6.6 method creates a descriptor table consisting of descriptor ranges for each resource type that is being used.&lt;/li&gt;
&lt;li&gt;SM6.6 allows for a practically empty root signature, as only root signature flags are required. &lt;em&gt;(Note Resource binding tier 3 is required)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, a &lt;em&gt;RenderResourceHandle&lt;/em&gt; is set using root constants to tell shaders what resources they need to use. This way of communicating resource indices is used both in the pre-SM6.6 and SM6.6 bindless approaches.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; bindless_params &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Push Constants
&lt;/span&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; push_constants &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_ROOT_PARAMETER1&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        ParameterType&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        ShaderVisibility&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_SHADER_VISIBILITY_ALL&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;Default&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;default&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; param_u &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; push_constants.u.Constants_mut&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    param_u.RegisterSpace &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;PushConstants.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;space_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    param_u.ShaderRegister &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    param_u.Num32BitValues &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PushConstantSlots&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;num_push_constant_slots&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;is_debug&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;push_constants&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Note that unlike VK, static samplers do not cause a bindless offset for the set they are bound to.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; static_samplers &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;create_static_sampler&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_FILTER_MIN_MAG_MIP_POINT&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_TEXTURE_ADDRESS_MODE_WRAP&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Add more static samplers if needed.
&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An important subject to think about is what tradeoffs are going to be made for sending render resource handles to the GPU. On one side you can choose to pre-determine the maximum amount of resources being indexed in any shader, and set the number of root constants accordingly. Instead of limiting the number of resources being indexed, you can also choose to create buffers containing render resource handles, adding more flexibility at the cost of an extra indirection. In short, you have two options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A maximum number of handles is determined that you will use in a shader.&lt;/li&gt;
&lt;li&gt;Instead of pre-determining the maximum number of handles, a buffer containing render resource handles can be made instead, only requiring a single push constant at the cost of an added indirection. For our framework Breda we chose this option.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once the root signature is created, it will be used to create every &lt;em&gt;Pipeline State Object&lt;/em&gt; and &lt;em&gt;State Object&lt;/em&gt; in the render pipeline, as every shader will now index the descriptor heap directly. An important thing to note for SM6.6 is that the &lt;em&gt;D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED&lt;/em&gt; flag is required for all pipelines that use descriptor heap indexing.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; desc_1_1 &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; desc.u.Desc_1_1_mut&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
desc_1_1.NumParameters &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; bindless_params.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
desc_1_1.pParameters &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; bindless_params.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_ptr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
desc_1_1.NumStaticSamplers &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; static_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
desc_1_1.pStaticSamplers &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; static_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_ptr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
desc_1_1.Flags &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the case of using DXR 1.0, local root signatures should not be used anymore, instead we only need to use the global root signature as this already allows us to retrieve any resource we might need.&lt;/p&gt;
&lt;h3 id=&quot;shader-model-6-5-and-lower&quot;&gt;Shader model 6.5 and lower&lt;/h3&gt;
&lt;p&gt;This shows the difference between the modern SM6.6 and the previous pre-SM6.6 approach.&lt;/p&gt;
&lt;p&gt;Just like the SM6.6 approach, the SM6.5 and lower root signature is also shared between all pipelines, but requires some extra information to be provided to the API in order to make it all work.&lt;/p&gt;
&lt;p&gt;One of the big constraints of DXIL and D3D12, is that registers cannot be overlapped. To work around this problem simply assign a register space for each descriptor table, all starting at &lt;em&gt;BaseShaderRegister 0&lt;/em&gt;. We also indicate that the descriptors and the data behind them are volatile. Besides a significant difference in shadercode, this is the only big difference on the CPU between SM6.6 and &amp;lt; SM6.5 bindless rendering.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; RWTexture2D
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; param_u &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    bindless_params&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;RwTexture2d.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;space_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
        .u
        .DescriptorTable_mut&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
param_u.NumDescriptorRanges &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
param_u.pDescriptorRanges &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_RANGE1&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    RangeType&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_RANGE_TYPE_UAV&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    NumDescriptors&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;std&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;u32&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    BaseShaderRegister&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Assign a unique register space for each resource type.
&lt;/span&gt;    RegisterSpace&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;RwTexture2d.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;space_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    Flags&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    OffsetInDescriptorsFromTableStart&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The catch of the previous technique is that we cannot use &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt;, but we can declare unsized arrays of resources at different registers to achieve the same thing. Since all of these registers start from index 0 and point to the same descriptor heap, we can essentially do the same here with resource indexing as we would do with &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt;. The main difference is that you need to be very explicit about what resource type you want to access, as indexing a wrong resource type will result in a crash or undefined behavior. Note that for every resource type, a new descriptor range needs to be declared.&lt;/p&gt;
&lt;h3 id=&quot;managing-bindless-descriptors&quot;&gt;Managing bindless descriptors&lt;/h3&gt;
&lt;p&gt;If we follow the idea behind bindless rendering where every resource handle has an index to the descriptor heap, we would need to solve the problem of &lt;em&gt;SRV&lt;/em&gt; and &lt;em&gt;UAV&lt;/em&gt; descriptors being separate descriptors. To solve this problem in D3D12 we introduce a &lt;em&gt;descriptor pair&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-struct syntax-rust&quot;&gt;struct&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-rust&quot;&gt;RenderResourceHandlePair&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;srv&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceHandle,
    &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;uav&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceHandle,
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The idea behind a &lt;em&gt;RenderResourceHandlePair&lt;/em&gt; is quite simple; we allocate SRV’s at descriptor index [&lt;em&gt;n&lt;/em&gt;], and allocate the associated UAV at descriptor index [&lt;em&gt;n + 1&lt;/em&gt;]. This way a single handle can represent both the &lt;em&gt;SRV&lt;/em&gt; and &lt;em&gt;UAV&lt;/em&gt; descriptors without having the need of keeping track of 2 separate handles. This handle is later interpreted in shader code for read(heap index [&lt;em&gt;n&lt;/em&gt;]) or write(heap index [&lt;em&gt;n + 1&lt;/em&gt;]) operations.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;allocate_buffer_handle_pair&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; RenderResourceHandlePair&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.pool&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;allocate_descriptor_pair&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceTag&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When resources are dropped we want to recycle the associated stale descriptor handles within our bindless heap. We do this by pushing stale descriptors on a &lt;a href=&quot;https://doc.rust-lang.org/std/collections/struct.VecDeque.html&quot;&gt;&lt;em&gt;VecDeque&lt;/em&gt;&lt;/a&gt;. Note that recycling descriptors may only happen when the stale descriptors are no longer in-flight on the GPU.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-other syntax-rust&quot;&gt;crate&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;retire_handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceHandle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.available_recycled_descriptors
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If a recycled descriptor is available we want to ensure that stale descriptors are reused in FIFO order; In the case of more resources being dropped compared to resource allocations being done, we could end up with unused descriptor indices that may not be reclaimed at all.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;allocate_descriptor_pair&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceTag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; RenderResourceHandlePair&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.available_recycled_descriptors
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;pop_front&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map_or_else&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; No recycled descriptors available.
&lt;/span&gt;                &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; descriptor_idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;increment_descriptor_pair&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceHandlePair&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; tag&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; descriptor_idx&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;recycled_handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Use old descriptor, update descriptor version and resource tag for validation.
&lt;/span&gt;                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceHandlePair&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new_from&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                    recycled_handle.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bump_version_and_update_tag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;preparing-the-command-buffer&quot;&gt;Preparing the command buffer&lt;/h3&gt;
&lt;p&gt;Unlike bindful, we just need to set our descriptor heap and root signature once per command list. This is done when the command list starts recording. It is important to set the descriptor heap before setting the bindless graphics and compute root signatures, this is due to the new ordering constraint introduced with SM6.6. This ordering constraint is to ensure that correct heap pointers are available when root signatures are set. It’s also important to make sure you’re only setting the graphics root signature when recording on a &lt;em&gt;D3D12_COMMAND_LIST_TYPE_DIRECT&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;begin&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.command_list_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_COMMAND_LIST_TYPE_DIRECT&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.command_list_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_COMMAND_LIST_TYPE_COMPUTE&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; cbv_srv_uav_heap_handle &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.descriptor_pool.pool
                &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
                .handle
                .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_ptr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                .SetDescriptorHeaps&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;cbv_srv_uav_heap_handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
                    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_mut_ptr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                 .SetComputeRootSignature&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.descriptor_pool.global_rs&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.command_list_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_COMMAND_LIST_TYPE_DIRECT&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                 &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                 .SetGraphicsRootSignature&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.descriptor_pool.global_rs&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;vulkan-setup&quot;&gt;Vulkan setup&lt;/h2&gt;
&lt;h3 id=&quot;descriptorset-setup&quot;&gt;Descriptorset setup&lt;/h3&gt;
&lt;p&gt;First off we need to make sure we have the correct &lt;em&gt;VkPhysicalDeviceDescriptorIndexingFeatures&lt;/em&gt; flags set up for our device to support this bindless setup, in our case we enable every flag except for &lt;em&gt;shaderInputAttachmentArrayDynamicIndexing&lt;/em&gt;, &lt;em&gt;shaderInputAttachmentArrayNonUniformIndexing&lt;/em&gt; and &lt;em&gt;descriptorBindingUniformBufferUpdateAfterBind&lt;/em&gt; which are not supported for older hardware.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that VK_EXT_descriptor_indexing is available since Vulkan 1.0.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It should be noted that Vulkan currently does NOT support &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; yet, but just like the D3D12 implementation, there are a few workarounds to achieve the same behavior or even emulate the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt;’s behavior. In part 2 of this series I will also cover how to emulate &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; for a more unified shader implementation between D3D12 and Vulkan.&lt;/p&gt;
&lt;p&gt;Unlike D3D12, Vulkan hides the descriptor pool within the API which forces users to follow a bindful descriptor set mindset. This makes bindless slightly more complicated to set up but not impossible.&lt;/p&gt;
&lt;p&gt;The resources that we want to use for bindless rendering are buffers, textures, acceleration structures, and samplers -which means using descriptor set types of &lt;em&gt;BUFFER, SAMPLED_IMAGE, STORAGE_IMAGE, ACCELERATION_STRUCTURE_KHR,&lt;/em&gt; and &lt;em&gt;SAMPLER&lt;/em&gt;. The Vulkan 1.2 spec guarantees minimum support of 4 descriptor sets, however current desktop GPU’s that support ray tracing guarantee a minimum of 32 descriptor sets. Seeing we want to set up 5 descriptor sets, this forces us to make compromises to guarantee support on the widest variety of devices. In our case we decided to drop bindless samplers and go for immutable samplers to limit our bindless sets to the guaranteed minimum of 4.&lt;/p&gt;
&lt;p&gt;Now that we are aware of the constraints we can proceed to setting up the &lt;em&gt;VkDescriptorPool&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Returns all descriptor pool sizes, SAMPLED_IMAGE will have extra space for immutable samplers.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; descriptor_sizes &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;descriptor_pool_sizes&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;immutable_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; descriptor_pool_info &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorPoolCreateInfo&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;builder&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;pool_sizes&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;descriptor_sizes&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorPoolCreateFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;UPDATE_AFTER_BIND&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;max_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each descriptor type we set up a &lt;em&gt;VkDescriptorPoolSize&lt;/em&gt; with the &lt;em&gt;descriptorCount&lt;/em&gt; set to the bindless pool size. The maximum supported count of each descriptor type can be retrieved from the &lt;em&gt;VkPhysicalDeviceLimits&lt;/em&gt;. In our case we also associate immutable samplers with our &lt;em&gt;SAMPLED_IMAGE&lt;/em&gt; pool size, so we need to also take these descriptors into account.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;descriptor_pool_sizes&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;immutable_sampler_count&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorPoolSize&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; type_histogram &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;std&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;collections&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;HashMap&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; For each descriptor type, retrieve the bindless size.
&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;for&lt;/span&gt; table &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;all_tables&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        type_histogram
            .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;to_vk&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;and_modify&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt;v &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;table_size&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;or_insert_with&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;table_size&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Add immutable sampler descriptors to texture descriptor pool size.
&lt;/span&gt;    type_histogram
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Texture.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;to_vk&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;and_modify&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt;v &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; immutable_sampler_count&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    type_histogram
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;ty&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;descriptor_count&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorPoolSize &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            ty&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt;ty&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt;descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;collect&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorPoolSize&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each resource type we are creating a descriptor set layout with the following flags: &lt;em&gt;VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT&lt;/em&gt;, &lt;em&gt;VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT&lt;/em&gt; and &lt;em&gt;VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT.&lt;/em&gt; Since we are using immutable samplers, we would need to assign these to one of the descriptor sets that is going to be used. In this case the most sensible thing is to associate them with the &lt;em&gt;SAMPLED_IMAGE&lt;/em&gt; descriptor set -this requires the &lt;em&gt;binding&lt;/em&gt; index to be set to the amount of immutable samplers that are going to be used. Every other descriptor set is set to &lt;em&gt;binding&lt;/em&gt; index 0. Afterwards we simply allocate the descriptor sets using these bindless layouts.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;create_bindless_layout&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Arc&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ash&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Device&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;immutable_samplers&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;[&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Sampler],
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;bool&lt;/span&gt;,
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorSetLayout&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;PipelineLayout&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; descriptor_layouts &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;all_tables&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;set_idx&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &amp;amp;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;table&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;set_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; set_idx&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; descriptor_binding_flags &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorBindingFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;PARTIALLY_BOUND&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorBindingFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;VARIABLE_DESCRIPTOR_COUNT&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorBindingFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;UPDATE_AFTER_BIND&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; set &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorSetLayoutBinding &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                binding&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                descriptor_type&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;to_vk&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; table.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;table_size&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                stage_flags&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderStageFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                p_immutable_samplers&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;std&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ptr&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;null&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; table &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Texture &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                descriptor_binding_flags.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorBindingFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;empty&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Set texture binding start at the end of the immutable samplers.
&lt;/span&gt;                set&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;.binding &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; immutable_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                set.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorSetLayoutBinding &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                    binding&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    descriptor_type&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;SAMPLER&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; immutable_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    stage_flags&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderStageFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    p_immutable_samplers&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; immutable_samplers.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_ptr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

            &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; ext_flags &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorSetLayoutBindingFlagsCreateInfoEXT&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;builder&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;binding_flags&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;descriptor_binding_flags&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

            device
                .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;create_descriptor_set_layout&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorSetLayoutCreateInfo&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;builder&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bindings&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;set&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorSetLayoutCreateFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;UPDATE_AFTER_BIND_POOL&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push_next&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; ext_flags&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;collect&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; num_push_constants &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PushConstantSlots&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;num_push_constant_slots&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;debug&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; num_push_constants_sized &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;std&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;mem&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;size_of&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; num_push_constants&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; push_constant_range &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ash&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;PushConstantRange &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        stage_flags&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderStageFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        offset&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        size&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; num_push_constants_sized&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; push_constant_ranges &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;push_constant_range&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; layout_create_info &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PipelineLayoutCreateInfo&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;builder&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;set_layouts&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;layouts&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push_constant_ranges&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;push_constant_ranges&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; pipeline_layout &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; device.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;create_pipeline_layout&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;layout_create_info&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;None&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Failed creating pipeline layout.&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;descriptor_layouts&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; pipeline_layout&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the pipeline layout we want to use the previously created descriptor set layouts and set up the root constants. In order to notify the shader what resources it’s supposed to index, we use push constants that we will use to send render resource handles to the GPU. Just like the D3D12 implementation there are two options to consider here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Determine a maximum number of handles you can bind in a shader and increase the number of push constants by that number.&lt;/li&gt;
&lt;li&gt;Instead of pre-determining the maximum number of handles, a buffer containing render resource handles can be made, only requiring a single push constant but also adding another indirection. We chose this option for our Breda framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;managing-bindless-descriptors-2&quot;&gt;Managing bindless descriptors&lt;/h3&gt;
&lt;p&gt;Allocating bindless descriptors is done per heap type, in our case we have 4 descriptor types that we can allocate: &lt;em&gt;STORAGE_BUFFERS, SAMPLED_IMAGE, STORAGE_IMAGE&lt;/em&gt; and &lt;em&gt;ACCELERATION_STRUCTURE_KHR&lt;/em&gt;. Unlike D3D12 we do not need to use descriptor pairs, instead SAMPLED and STORAGE images share the same descriptor index between heaps, reducing complexity. With an api-agnostic design and preparations for potential future API updates in mind, we decided to globally track resource indices instead of tracking per-heap. It also means that the bindless heaps will have gaps of unused descriptors. This is not as optimal compared to the D3D12 implementation which only uses a single bindless heap, and will likely be changed as soon as the official SM6.6 &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; support has been added to Vulkan. That being said, the bindless descriptor manager will still prevent unused reclaimable descriptor indices as much as possible.&lt;/p&gt;
&lt;p&gt;For each resource, a descriptor update is done on its corresponding descriptor set at &lt;em&gt;dst_binding&lt;/em&gt; 0, where we simply use our fetched descriptor handle index as &lt;em&gt;dst_array_element&lt;/em&gt; index. Afterwards the descriptor set is updated directly.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;allocate_buffer_handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;,
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer,
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; RenderResourceHandle&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; handle &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;fetch_available_descriptor&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceTag&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; buffer_info &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;DescriptorBufferInfo &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        buffer&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        offset&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        range&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;WHOLE_SIZE&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; write &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;WriteDescriptorSet &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        dst_set&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.sets&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BindlessTableType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;set_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        dst_binding&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        descriptor_count&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        dst_array_element&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; handle.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        descriptor_type&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;STORAGE_BUFFER&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        p_buffer_info&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;buffer_info&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;Default&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;default&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.device.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;update_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;write&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    handle
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just like the D3D12 implementation, when dropping resources that are out-of-flight, we want to recycle the associated descriptors. We do this by pushing these stale descriptors to a &lt;a href=&quot;https://doc.rust-lang.org/std/collections/struct.VecDeque.html&quot;&gt;&lt;em&gt;VecDeque&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;retire_handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceHandle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.available_recycled_descriptors
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If a recycled descriptor is available, we want to make sure that we re-use the first dropped descriptor. This way FIFO order is respected, preventing potential fragmentation. For example in the case of more resources being dropped compared to resource allocations being done, we could end up with unused descriptor indices that may not be reclaimed at all.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;fetch_available_descriptor&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;self&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceTag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; RenderResourceHandle&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.available_recycled_descriptors
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;pop_front&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map_or_else&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceHandle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; tag&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;increment_descriptor&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;recycled_handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;recycled_handle.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bump_version_and_update_tag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;preparing-the-command-buffer-2&quot;&gt;Preparing the command buffer&lt;/h3&gt;
&lt;p&gt;Now that the descriptor sets have been set up along with the pipeline layout, we can bind these descriptor sets and the pipeline layout once for the graphics, compute and ray tracing pipeline points.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.queue_flags.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;QueueFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;GRAPHICS&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.queue_flags.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;QueueFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;GRAPHICS&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.device.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd_bind_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.cmd&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PipelineBindPoint&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;GRAPHICS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                    .descriptor_pool
                    .bindless_pipeline_layout&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                    .descriptor_pool
                    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bindless_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.queue_flags.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;QueueFlags&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;COMPUTE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.device.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd_bind_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.cmd&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PipelineBindPoint&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;COMPUTE&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                .descriptor_pool
                .bindless_pipeline_layout&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                .descriptor_pool
                .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bindless_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.optional_handles.ray_tracing_pipeline.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;is_some&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.device.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;cmd_bind_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.cmd&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;PipelineBindPoint&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;RAY_TRACING_KHR&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                    .descriptor_pool
                    .bindless_pipeline_layout&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.allocation_handles
                    .descriptor_pool
                    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;bindless_descriptor_sets&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-bindless-resources&quot;&gt;Using bindless resources&lt;/h2&gt;
&lt;h3 id=&quot;cpu-side&quot;&gt;CPU side&lt;/h3&gt;
&lt;p&gt;To communicate resources to the GPU, we fill a buffer with &lt;em&gt;RenderResourceHandles&lt;/em&gt;, however we can immediately track resources states in this process along with placing appropriate memory barriers if needed. We achieve this by creating a &lt;code&gt;DescriptorSetBuilder&lt;/code&gt;, this indicates whether resources should be read from or written to, which allows us to preemptively determine if resource state tracking is required.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; gtao_set &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;DescriptorSetBuilder&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ephemeral&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;shader_db.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;get_pipeline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;gtao&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-language syntax-rust&quot;&gt;self&lt;/span&gt;.gtao_target&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; depth_stencil_target&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; normal_target&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;ssao_constants&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;device&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; dma_enc&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For our framework &lt;em&gt;Breda&lt;/em&gt; we aim to do as little resource tracking as possible, therefore we heavily rely on resource promotion and decay. However if explicit resource tracking is required, this can easily be added within this system. The following example is written for D3D12 but the same principle applies to Vulkan. We gather all of the &lt;em&gt;RenderResourceHandles&lt;/em&gt; out of the &lt;em&gt;DescriptorSetBuilder&lt;/em&gt; and put them in an array, which is then pushed to the GPU, in the form of a buffer.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; shader_bindings &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;
    bindings
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            binding.resource.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;as_ref&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map_or_else&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceHandle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;invalid&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;resource&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;resource&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;binding.access_type&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Resource&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;TopLevel&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;tlas&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;_&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; tlas.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Resource&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;AccessType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ReadOnly&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        buffer.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Resource&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Buffer&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;buffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;AccessType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ReadWrite&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; We don&amp;#39;t do transitions on buffers, except if the buffer is used for indirect args.
&lt;/span&gt;                        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; The transition to indirect_args is done inside the command_buffer when the buffer is passed in as a func parameter.
&lt;/span&gt;                        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; But the transition back to a non-indirect arg state is handled by the descriptor set instead.
&lt;/span&gt;                        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; dx12_buffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; buffer.&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;downcast_ref&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Dx12Buffer&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;BufferUsage&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;IndirectArgs &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; dx12_buffer.desc.usage &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                            indirect_args_buffer_state_transitions.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                                dx12_buffer.resource&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_RESOURCE_STATE_UNORDERED_ACCESS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                        &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; buffer.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Resource&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Texture&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;texture&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;AccessType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ReadOnly&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; dx12_texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; texture.&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;downcast_ref&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Dx12Texture&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        texture_state_transitions.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                            dx12_texture.resource&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-other syntax-rust&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;command_buffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;TEXTURE_PREFERRED_READ_STATE&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; texture.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                    &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Resource&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Texture&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;texture&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;AccessType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ReadWrite&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; dx12_texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; texture.&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;downcast_ref&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Dx12Texture&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        texture_state_transitions.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                            dx12_texture.resource&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;d3d12&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;D3D12_RESOURCE_STATE_UNORDERED_ACCESS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

                        binding.mip_index.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;map_or_else&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt; texture.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;mip_index&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;|&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-closure syntax-rust&quot;&gt;texture.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;mip_resource_handle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;mip_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                        &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
            &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;collect&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The buffer that contains our handles is a resource itself, which means it also has a &lt;em&gt;RenderResourceHandl&lt;/em&gt;e. We then use this buffer’s handle as our push/root constant for the draw or dispatch call.&lt;/p&gt;
&lt;p&gt;This constant is then interpreted by our shader code as a &lt;em&gt;RenderResourceHandle&lt;/em&gt;, which allows us to retrieve all of the necessary handles for that specific shader.&lt;/p&gt;
&lt;h3 id=&quot;shaders&quot;&gt;Shaders&lt;/h3&gt;
&lt;p&gt;Now that we have set our resource handle via a root/push constant, we have gained a way to communicate what resources the GPU should access. As discussed previously, we use a buffer containing an array of &lt;em&gt;RenderResourceHandles&lt;/em&gt; to index our descriptor heap. For the sake of this introduction to bindless, we kept the &lt;em&gt;RenderResourceHandle&lt;/em&gt; as simple as possible. In parts 2 and 3 of this series we will start modifying this &lt;em&gt;RenderResourceHandle&lt;/em&gt; to do a lot more than just contain just the index.&lt;/p&gt;
&lt;p&gt;In order to interpret the &lt;em&gt;RenderResourceHandles&lt;/em&gt;, we simply unpack them in the shader code and retrieve the handle that we can use to index the arrays of resources.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;RenderResourceHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; index&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#ifdef&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have the descriptor heap index, you can simply access the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; to retrieve your resource. Since this functionality is not officially supported yet on Vulkan, we need to do some declarations up front to take some user hassle away. In the case of the pre-SM6.6 D3D12 bindless approach, the same declarations are necessary.&lt;/p&gt;
&lt;p&gt;A very big constraint with using this technique is that we are currently forced to use &lt;code&gt;(RW)ByteAddressBuffers&lt;/code&gt; for all of our buffers. When wrapped nicely in strongly typed bindless resources (part 2 of this series), this doesn’t matter for shader code on the user’s end, however SPIR-V has some issues with performing vectorized loads on templated byte buffers. On DXIL this is not an issue at all though, but it is definitely a very important point to keep in mind.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;BindingsOffset&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; userData0&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; userData1&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; userData2&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Resource bindings, same structure applies for any other resource types and their declerations.
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; For this example we will just declare a simple bindless Texture2D
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#ifdef&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;push_constant&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; ConstantBuffer&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;BindingsOffset&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; g_bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; SamplerState g_samplerState&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; ByteAddressBuffer g_byteAddressBuffer&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; RWByteAddressBuffer g_rwByteAddressBuffer&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;BINDLESS_TEXTURE_2D_DECL&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                                                                &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; g_texture2d&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    SamplerState &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;samplerMinMagMipPointWrap&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; g_samplerState&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;texture2D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; g_texture2d&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;NonUniformResourceIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;byteBufferUniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; g_byteAddressBuffer&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;rwByteBufferUniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; g_rwByteAddressBuffer&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; D3D12 Bindless&lt;/span&gt;
&lt;/span&gt;    ConstantBuffer&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;BindingsOffset&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; g_bindingsOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-c++&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;b0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; space10&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    SamplerState g_samplerMinMagMipPointWrap &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-c++&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;s0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; space0&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This declaration is ignored on D3D12.
&lt;/span&gt;    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;BINDLESS_TEXTURE_2D_DECL&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    SamplerState &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;samplerMinMagMipPointWrap&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; g_samplerMinMagMipPointWrap&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;texture2D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;NonUniformResourceIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;byteBufferUniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;rwByteBufferUniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; T&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; T &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;loadBindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    T result &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;byteBufferUniform&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;g_bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;bindingsOffset&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;T&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the shader we would include our bindless helper and declare the resources we want to use.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-include syntax-c++&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;breda-render-backend-api::bindless.hlsl&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Simple bindless copy shader example.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Bindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle input&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    RenderResourceHandle output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; threadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;SV_DispatchThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    Bindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;loadBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;Bindings&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; output &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;byteBuffer&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;input&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;rwByteBufferUniform&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;output&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Store&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; output&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;next-blog&quot;&gt;Next blog&lt;/h2&gt;
&lt;p&gt;In the next part of this series we will cover how to turn this bindless system into a modern templated bindless pipeline, along with showing some tricks to emulate &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; behavior in Vulkan.&lt;/p&gt;
</description></item><item><title>Bindless rendering: Templates</title><link>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-templates</link><guid>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-templates</guid><description>&lt;p&gt;In this continuation of &lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&quot;&gt;bindless rendering: setup&lt;/a&gt;, we now shift our focus to using bindless resources in shaders. Previously we were using &lt;em&gt;RenderResourceHandles&lt;/em&gt; to index the descriptor heap, but this still forces the user to write unnecessary code that can be neatly wrapped. You can find the other parts of the series here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&quot;&gt;Part 1: Bindless rendering: setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-validation-ffcae4fdbe1e&quot;&gt;Part 3: Bindless rendering: validation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ideally we would like to access our resources directly from a buffer, without the user explicitly having to call &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; or even touch resource bindings in general.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Bindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    Texture2D input&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    RWTexture2D output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This behavior is currently not possible in HLSL, however we can achieve something close to this by writing a wrapper around the resource types. We will cover the methods used to achieve the desired behavior using templated wrappers for resources. This approach requires SPIR-V &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; indexing functionality, which is currently not supported, but a few tricks can be used to emulate roughly the same behavior. These tricks to emulate &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; behavior in SPIR-V will be covered as well.&lt;/p&gt;
&lt;p&gt;Note that the techniques we’re going to discuss require the latest DXC build.&lt;/p&gt;
&lt;p&gt;Relevant topics that were covered in the previous part: We covered how registers can be overlapped on Vulkan in order match our resource indices between Dx12 and Vulkan. We also introduced the &lt;em&gt;RenderResourceHandle&lt;/em&gt; which represents our descriptor index within the descriptor heap.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;RenderResourceHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; index&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#ifdef&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt;
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;wrapping-resources&quot;&gt;Wrapping resources&lt;/h2&gt;
&lt;p&gt;To achieve our goal of being able to load any resource from an arbitrary piece of memory, we need to wrap our resources in such a way that we keep to a specific set of rules. First off we must make sure we are not adding any data on top of the &lt;em&gt;RenderResourceHandle&lt;/em&gt; that we are wrapping, as we heavily rely on our 32 bit constants in order to communicate our resource index buffer to the GPU.&lt;/p&gt;
&lt;p&gt;We then need to ensure that we wrap all resource types that the user may want to use, this is quite a big list of resources and functionality. Thankfully since the recent addition of templates to HLSL, we can greatly reduce the amount of code needed to add support for all these resources. But this is still a big task to do as we need to support all functionality within each wrapped resource.&lt;/p&gt;
&lt;p&gt;Finally we need to make the interface as intuitive as possible for the user, the whole point of these resource wrappers is to make the user’s life easier!&lt;/p&gt;
&lt;p&gt;We start off by defining the types we want to wrap, for our case we decided to create the following types that will replace our &lt;em&gt;RenderResourceHandles&lt;/em&gt; in our resource indices buffer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;AccelerationStructure (Represents Tlas)&lt;/li&gt;
&lt;li&gt;RawBuffer (Represents ByteAddressBuffer)&lt;/li&gt;
&lt;li&gt;RWRawBuffer (Represents RWByteAddressBuffer)&lt;/li&gt;
&lt;li&gt;ArrayBuffer (Represents StructuredBuffer)&lt;/li&gt;
&lt;li&gt;RWArrayBuffer (Represents RWStructuredBuffer)&lt;/li&gt;
&lt;li&gt;Texture (Represents all Texture types)&lt;/li&gt;
&lt;li&gt;RWTexture (Represents all RWTexture types)&lt;/li&gt;
&lt;li&gt;SimpleBuffer (Represents a single element buffer)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You could choose to separately wrap textures based on dimension and type instead but this would result in an increase of boiler plate code. We weighed the pros and cons of having separately wrapped texture types compared to a single wrapper, and decided to go for a single wrapper instead. Both options are perfectly viable, but we preferred the option that required the least amount of maintenance.&lt;/p&gt;
&lt;p&gt;We had to create a macro to minimize code divergence between Dx12 and Vulkan when accessing resources. In short &lt;em&gt;DESCRIPTOR_HEAP&lt;/em&gt; maps directly to the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; in Dx12, but maps to our own &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; emulation in Vulkan.&lt;/p&gt;
&lt;p&gt;As for the implementation, all of the associated functions need to be wrapped for each of the types. For buffer types this is very straightforward, we just implement the &lt;em&gt;Load&lt;/em&gt; and &lt;em&gt;Store&lt;/em&gt; functions (In case of RW variant).&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;ArrayBuffer&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle handle&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; ReadStructure&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; ReadStructure &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;load&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        ByteAddressBuffer buffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandle&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        ReadStructure result &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; buffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;ReadStructure&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ReadStructure&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; ReadStructure&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; ReadStructure &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; UNIFORM access here means that we’re internally not accessing the descriptorheap with NURI.
&lt;/span&gt;        ByteAddressBuffer buffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP_UNIFORM&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandleUniform&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        ReadStructure result &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; buffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;ReadStructure&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ReadStructure&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, for texture types this becomes a bit more complicated as we need to take resource dimensions and type into account. So we need to implement a Load, Store, Sample etc. for all of the possible resource dimensions, along with adding support for any other texture types, for example a &lt;em&gt;TextureCube&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Texture&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle handle&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Note that this example still misses all of the other wrapped function calls.
&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; TextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; TextureValue &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;load2D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint2 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;Texture2DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; texture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;uint3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; TextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; TextureValue &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;load3D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        Texture3D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;Texture3DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; texture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;uint4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; TextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    TextureValue &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;sampleLevel2D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;SamplerState &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; float2 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;mip&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;Texture2DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; texture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;SampleLevel&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;s&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; uv&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; mip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; TextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    TextureValue &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;sampleLevel3D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;SamplerState &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; float3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;mip&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        Texture3D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;Texture3DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;TextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; texture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;SampleLevel&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;s&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; uv&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; mip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An important thing to note here is that some functions might not be exposed to certain pipeline stages, you can choose to explicitly hide certain function implementations by checking what shader stage we are currently in. These shader stages defines are exposed by default in HLSL both in DXIL and SPIR-V.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_VERTEX
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_PIXEL
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_GEOMETRY
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_HULL
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_DOMAIN
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_COMPUTE
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_AMPLIFICATION
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_MESH
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; __SHADER_TARGET_STAGE == __SHADER_STAGE_LIBRARY
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For RW textures we cannot directly overload the array operator to store data, so instead we wrap its functionality to a function, again we specialize per texture dimension here.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;RwTexture&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle handle&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This should also implements load functions
&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; RWTextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint2 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RWTextureValue &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        RWTexture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;RWTextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;RWTexture2DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;RWTextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        texture&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;pos&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; value&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; RWTextureValue&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;store3D&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RWTextureValue &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        RWTexture3D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;RWTextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; texture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;RWTexture3DHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;RWTextureValue&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        texture&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;pos&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; value&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;resourcedescriptorheap-in-vulkan&quot;&gt;ResourceDescriptorHeap in Vulkan&lt;/h2&gt;
&lt;p&gt;At this time Vulkan doesn’t support the recently introduced &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; yet. Since we are running the same shaders in both Dx12 and Vulkan, we need to find a way to emulate the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; in such a way that we have minimal divergence in code paths between DXIL and SPIR-V resource accesses.&lt;/p&gt;
&lt;p&gt;We found three viable options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Write a static wrapper that emulates &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; as close as possible with indexing operator overloading.&lt;/li&gt;
&lt;li&gt;Create a define that accesses the resources according to some explicit arguments provided.&lt;/li&gt;
&lt;li&gt;Wait for the official &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; support to drop for Vulkan/SPIR-V&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Waiting for the official Vulkan extension to be released was out of the question for us, so we decided to try the two other options instead for the time being. Both of these options have their pros and cons in regards to code quantity and emulation accuracy.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Important note&lt;/strong&gt;&lt;/em&gt;: The techniques that we are going to cover to emulate &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; behavior in SPIR-V, are NOT meant to be used as a long-term solution. We’re just showing that certain paths are possible within SPIR-V to roughly achieve this functionality. Once the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; is officially supported, all of the SPIR-V specific boilerplate code can be thrown away for a much more light-weight implementation.&lt;/p&gt;
&lt;p&gt;First off, there are a set of texture types that we need to support in our framework, which of types &lt;em&gt;uint&lt;/em&gt;, &lt;em&gt;int&lt;/em&gt; and &lt;em&gt;float&lt;/em&gt;. Remember &lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&quot;&gt;that registers can be overlapped in Vulkan&lt;/a&gt;, we abuse this behavior to create macros that declare all of our texture resources, including their value types.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;textureType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;bindingA&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;bindingB&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                    &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;bindingA&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; bindingB&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; textureType&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;                                         &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        g##_##textureType##&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;                                                                &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;bindingA&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; bindingB&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; textureType&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;float2&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;                                        &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        g##_##textureType##float2&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Repeat this line for all resource types and their value types you want to support
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s assume we call this with &lt;code&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS(Texture2D, 0, 0)&lt;/code&gt;; this would be expanded to the following:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; g_Texture2Dfloat&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;vk&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;binding&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;float2&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; g_Texture2Dfloat2&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we fill out all the value type implementations in the macro, we can easily declare all of our texture registers without having to manually type out hundreds of lines of code. Setting up the registers using the texture types and their value types makes it especially convenient later on to match resource access with the corresponding registers as we can match this in the indexing overloading implementation of the &lt;em&gt;VkResourceDescriptorheap&lt;/em&gt; emulation. Now that we have a define that defines all value types of a texture type, we can simply call this implementation macro for all of the texture types.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;Texture1D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;Texture2D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;Texture3D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;TextureCube&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; NUM_STATIC_SAMPLERS&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;RWTexture1D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;RWTexture2D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;DEFINE_TEXTURE_TYPES_AND_FORMATS_SLOTS&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;RWTexture3D&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; etc…
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;operator-overloading-method&quot;&gt;Operator overloading method&lt;/h3&gt;
&lt;p&gt;The operator overloading method is by far the most complex method that we found, but seems to be the option closest to emulating &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; behavior in SPIR-V.&lt;/p&gt;
&lt;p&gt;For the index operator overload method, we simply create a new struct and implement all the operator overloads based on what resource type is being retrieved. There is a big problem with this though, which is that operator overloading does not differentiate based on return type but only on arguments. This forces us to write a dummy identifier for each resource access type. For buffers we do not need to specify a template argument as this type does not rely on them when doing any kind of resource access. Textures are a bit of a different story though, as they can contain different types of types, therefore we need to add a template type to each of the handles.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; A few examples of these dummy types
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;ByteBufferHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; internalIndex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; T&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Texture2DHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; internalIndex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; T&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;RWTexture2DHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; internalIndex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again we abuse macros here to reduce the amount of code until &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; is officially supported. We define an operator overload specialization based on the resource type and its value type.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;TEXTURE_TYPE_TEMPLATE_SPECIALIZATION_DECL&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;registerName&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;valueType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
                                                  &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handleName&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                                      &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    resourceType&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;valueType&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;handleName&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;valueType&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; identifier&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;                         &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; registerName##valueType&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;NonUniformResourceIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;identifier&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;internalIndex&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;          &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Since this follows the same pattern as our resource register declarations, we can easily match it to the correct register.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After defining how each operator overload should be implemented, we can create another macro that implements all index operator overloads for all texture types, and their texture value type permutations.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;TEXTURE_TYPE_TEMPLATE_SPECIALIZATION_DECL_MULTI&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                              &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;TEXTURE_TYPE_TEMPLATE_SPECIALIZATION_DECL&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;resourceType&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; g##_##resourceType&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;             \
                                              resourceType##&lt;span class=&quot;syntax-support syntax-type syntax-mac-classic syntax-c&quot;&gt;Handle&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;                                &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Repeat the TEXTURE_TYPE_TEMPLATE_SPECIALIZATION_DECL for each texture value type you want to support.&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have a way to implement the index operator overloading, we can easily start adding support for types we want to support in our &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; emulation. Note that we assume &lt;em&gt;(RW)ByteAddressBuffers&lt;/em&gt; for all of our buffer accesses. At the time of writing this blog, there are still a few issues with getting &lt;em&gt;StructuredBuffers&lt;/em&gt; working correctly using this implementation. Once these issues have been solved, other buffer types can be added to this implementation as well.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;VulkanResourceDescriptorHeapInternal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    ByteAddressBuffer &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;operator[]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandle &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;identifier&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; g_ByteAddressBuffer&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;NonUniformResourceIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;identifier&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;internalIndex&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Implement all the operator overloading implementations here
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Implementing types goes by their resource type, for example Texture2D in this case.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-assumed-macro syntax-c&quot;&gt;    &lt;span class=&quot;syntax-variable syntax-function syntax-assumed-macro syntax-c&quot;&gt;TEXTURE_TYPE_TEMPLATE_SPECIALIZATION_DECL_MULTI&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;Texture2D&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Create a static instance of the resource heap
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-c++&quot;&gt;static&lt;/span&gt; VulkanResourceDescriptorHeapInternal VkResourceDescriptorHeap&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have roughly the same behavior with resource heap indexing in Vulkan compared to Dx12, there is one last step to make this fully work. We need to explicitly state what kind of a resource the &lt;em&gt;VkResourceDescriptorHeap&lt;/em&gt; is going to index. This is far from ideal and could potentially be worked around with more template magic, but we decided this would not be worth the effort.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#ifdef&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handleType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; VkResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;handleType&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handleType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;NonUniformResourceIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;pure-define-approach&quot;&gt;Pure define approach&lt;/h3&gt;
&lt;p&gt;Instead of choosing to mimic the &lt;em&gt;ResourceDescriptorHeap&lt;/em&gt; indexing, you can also choose to take a much more lightweight approach, that trades away code quantity for a split in the resource heap. This still uses the resource register declarations discussed earlier in this post. This technique is significantly more light-weight compared to the array operator overloading approach, but still is far from ideal.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#ifdef&lt;/span&gt; VK_BINDLESS
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;TEXTURE_DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;valueType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; g##_##resourceType##valueType&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;BUFFER_DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; g##_##resourceType&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;TEXTURE_DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;valueType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;BUFFER_DESCRIPTOR_HEAP&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;resourceType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; ResourceDescriptorHeap&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;handle&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;resulting-shader-code&quot;&gt;Resulting shader code&lt;/h2&gt;
&lt;p&gt;The templated resource wrappers lead to quite a big helper file, but opens up paths to writing very clean and intuitive shaders. Instead of having the user always write the same resource declaration and indexing logic for any resources they may need, we just have a buffer containing our resources, which are directly accessible without forcing the user to worry about registers and declarations. This is a very sharp double-edged sword though, this technique allows the user to load entire resources from any memory, which potentially may not contain a valid resource, resulting in a crash when these are accessed. For our framework Breda, we simply discourage this behavior unless absolutely necessary. Luckily there is a reliable way to validate these resource access patterns which will be covered in the last part of this series.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-include syntax-c++&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;breda-render-backend-api::templated_bindless.hlsl&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Constants&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; someUintIndex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; someTextureOffset&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; someRwArrayBufferOffset&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Bindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    Texture tex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    ArrayBuffer someBuffer&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    SimpleBuffer constants&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    RwTexture output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint2 pos
                                &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_DispatchThreadID&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
    Bindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;loadBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;Bindings&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Load constants here from a `SimpleBuffer`, this SimpleBuffer contains a single structured element,
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; therefore no offset/index is required here.
&lt;/span&gt;    Constants constants &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;loadUniform&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;Constants&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Retrieve some value from the ArrayBuffer, which essentially emulates a StructuredBuffer without being bound to a type.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; someValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someBuffer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someUintIndex&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We can also directly load resources from any piece of memory if needed.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This is VERY error prone, but may be extremely useful in cases of loading nested geometry or textures.
&lt;/span&gt;    Texture someTexture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someBuffer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;Texture&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someTextureOffset&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This can be done for any wrapped resource types, but again this easily breaks if a mistake is made.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; in part 3 of the blogpost series, we will cover how to detect invalid usecases if this is being used.
&lt;/span&gt;    RwArrayBuffer someRwArrayBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someBuffer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;RwArrayBuffer&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;someRwArrayBufferOffset&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We can then sample `someTexture` directly afterwards without taking additional steps
&lt;/span&gt;    float4 sampledValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; someTexture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;float4&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    float4 otherSampledValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;tex&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;float4&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    float4 outputValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; sampledValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; otherSampledValue&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Directly store the output
&lt;/span&gt;    bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;float4&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; outputValue&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;next-blog&quot;&gt;Next blog&lt;/h2&gt;
&lt;p&gt;In the final part of the series we will be covering bindless GPU validation built on top of everything we’ve covered so far. The GPU validation should cover the newly introduced risks by taking this templated approach. This includes validating invalid resource usage, ensuring a handle actually points to a resource and finally how to easily verify if a handle’s lifetime is valid.&lt;/p&gt;
</description></item><item><title>Bindless rendering: validation</title><link>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-validation</link><guid>https://www.evolvebenchmark.com/blog-posts/bindless-rendering-validation</guid><description>&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&quot;&gt;Part 1: Bindless rendering: setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-templates-8920ca41326f&quot;&gt;Part 2: Bindless rendering: templates&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With a bindless rendering pipeline in place as covered in the previous blogs, we are now faced with the problem of not being able to validate resource access yet. At the moment (of writing this blog) the validation layer both on Dx12 and Vulkan are not capable of detecting or even preventing all of the bindless pitfalls yet, additionally the existing GPU validation layers are slow.&lt;/p&gt;
&lt;p&gt;The goal is to inform the user of any invalid usage at runtime and attempt to avoid potential page faults when invalid descriptors are indexed, along with not absolutely killing performance. In this final part of the bindless rendering series, we will cover what measures can be taken to reach these validation goals, while not exploding the rendering backend with intrusive tracking code. The techniques covered here are not “bulletproof”, but should catch the majority of potential issues when using a bindless rendering pipeline.&lt;/p&gt;
&lt;h2 id=&quot;renderresourcehandle-recap&quot;&gt;RenderResourceHandle recap&lt;/h2&gt;
&lt;p&gt;In the previous blogposts we covered what our RenderResourceHandles look like, along with how we don’t necessarily need to use all 32 bits of such a handle to index the resource descriptor heap. Instead of using all 32 bits for just the index in the descriptorheap, we use the following structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;23 bits for the actual resource index&lt;/li&gt;
&lt;li&gt;2 bits to identify the resource type&lt;/li&gt;
&lt;li&gt;1 bit for resource writability&lt;/li&gt;
&lt;li&gt;6 bits for resource handle version&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; A handle that points to a rendering related resource (TLAS, Sampler, Buffer, Texture etc)
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; this handle can be uploaded directly to the GPU to refer to our resources in a bindless
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; fashion and can be plainly stored in buffers directly - even without the help of a [`DescriptorSet`]
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; the handle is guaranteed to live as long as the resource it&amp;#39;s associated with so it&amp;#39;s up to
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; the user to ensure that their data lives long enough. The handle is versioned to prevent
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; use-after-free bugs however.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;derive&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;Copy&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Clone&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Eq&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; PartialEq&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; Hash&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;transparent&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-struct syntax-rust&quot;&gt;struct&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-rust&quot;&gt;RenderResourceHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-impl syntax-rust&quot;&gt;impl&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-impl syntax-rust&quot;&gt;RenderResourceHandle&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-impl syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u8&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; RenderResourceTag, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;, &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;access_type&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; AccessType&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; version &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; version &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; tag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; tag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; index&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; access_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; access_type.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;is_read_write&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;assert!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;version &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;64&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; version wraps around, it&amp;#39;s just to make sure invalid resources don&amp;#39;t get another version
&lt;/span&gt;        &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;assert!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;tag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-hexadecimal syntax-rust&quot;&gt;0x3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;assert!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;23&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;version &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;26&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; access_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; tag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;|&lt;/span&gt; index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;validating-the-resource&quot;&gt;Validating the resource&lt;/h2&gt;
&lt;p&gt;We use the validation bits from the resource handle directly in the resource abstraction, where we then explicitly validate what kind of resource we are expecting, along with if the resource should be writable.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RenderResourceHandle &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; expectedTag &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;resourceTag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;writable&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;isWritable&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passed_validation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; passedWritableValidation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Write out a small validation packet to a readback buffer
&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Finally return if the validation succeeded
&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; passed_validation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These packets are then read back from the GPU to the CPU, since we blocked any reads/writes on the GPU, a guarantee has been given that the data will always reach the CPU as device removal has been avoided. The readback buffer can also be conveniently used for shader prints or shader asserts, as the logic to achieve this is exactly the same as writing out validation packets. Once the buffer is available on the CPU, we process all of the packets based on the atomic that is located at the start of the buffer. This atomic indicates how many readback packets have been written, we then read out the header of each packet to determine what kind of message we’re dealing with.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;process_packets&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;packet_buffer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Arc&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;dyn Buffer&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;command_tracker&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;CommandTracker,
    &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;logging_info&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;GpuLogInfo,
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; No need to use a deferred readback buffer as the command buffer has already completed at this point.
&lt;/span&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; packets &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; packet_buffer.&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;as_slice&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;ShaderLogPacket&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; 0th element contains our atomic counter, containing message count.
&lt;/span&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; num_messages &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; packets&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;.header &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;MAX_ALLOWED_SHADER_MESSAGES&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        packets&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;.header
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;warn!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Exceeding maximum supported GPU logging packets, please increase `MAX_ALLOWED_SHADER_MESSAGES` if needed.
        Number of messages: {} Maximum allowed: {}&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; packets&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;.header&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;MAX_ALLOWED_SHADER_MESSAGES&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-constant syntax-other syntax-rust&quot;&gt;MAX_ALLOWED_SHADER_MESSAGES&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; The shader packets start at index 1 as index 0 is reserved for atomics.
&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;for&lt;/span&gt; message_idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;num_messages &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; current_packet &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; packets&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;message_idx &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Read shader message header.
&lt;/span&gt;        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; shader_header &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderHeaderData&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;extract_data_from_header&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;current_packet.header&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; name &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; shader_header.pipeline_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; command_tracker.tracked_commands.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;command_tracker.tracked_commands&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;shader_header.pipeline_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Untracked command&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;match&lt;/span&gt; shader_header.ty &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderMessageType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Validation &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; logging_info.gpu_resource_validation &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; validation_type &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt;
                        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderValidationType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;try_from&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;extract_bits&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;current_packet.data0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                            .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Received invalid shader validation packet&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; validation_handle &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceHandle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;internal_from_raw&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;extract_bits&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                            current_packet.data0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;match&lt;/span&gt; validation_type &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderValidationType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ResourceTag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; expected_resource_tag &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;RenderResourceTag&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;try_from&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;extract_bits&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                                current_packet.data0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                            .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Invalid resource tag&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                            &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;error!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;{} GPU resource validation failed: Resource access mismatch in `{}` handle is of type: `{:?}`, Expected handle of type: `{:?}`.&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                                shader_header.identifier&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; validation_handle.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; expected_resource_tag
                            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderValidationType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;ResourceAccess &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;error!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;{} GPU resource validation failed: Tried writing to resource that is read-only in `{}` RenderResourceHandle has AccessType of: `{:?}`.&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                                shader_header.identifier&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; validation_handle.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;access_type&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
                        &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderValidationType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Invalid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt;
                    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

                    &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;panic!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
                &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;ShaderMessageType&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Invalid &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
            &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have placed measures against invalid resource indexing, we should be done right? Well not quite, what if a buffer on the GPU contains a handle that has already been dropped? This scenario would pass the resource type validation along with the writability, while not being valid for use. Tracking this scenario from the CPU quickly becomes a nightmare if multiple buffers contain resource handles (for example materials that directly contain their resource handles).&lt;/p&gt;
&lt;p&gt;Finally there’s also the issue that the user could potentially copy resource handles on the GPU from one buffer to another. These scenarios quickly become frustratingly difficult if not impossible to deal with, so we need a different solution for these edge cases.&lt;/p&gt;

&lt;h2 id=&quot;creating-a-mirror-resource-heap&quot;&gt;Creating a mirror resource heap&lt;/h2&gt;
&lt;p&gt;A way to deal with these situations is by keeping track of the version of the resource handle. Every time a resource handle is recycled on the CPU, the version is incremented and looped around by 64 (6 bits). Recycling a handle happens as soon as the resource is dropped on the CPU.&lt;/p&gt;
&lt;p&gt;Right before commandbuffer submission, we copy our entire updated pool of &lt;em&gt;RenderResourceHandles&lt;/em&gt; to the GPU. As this pool is contained within a buffer, we can use the buffer’s &lt;em&gt;RenderResourceHandle&lt;/em&gt; directly to set a dedicated push constant slot, which is global to all of our pipelines. We then use this buffer to read out the actual handle version, and compare this against the target resource handle’s version.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RenderResourceHandle &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Make sure the version of the handle is the same as the reflected version from the CPU.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; If we have a mismatch here that means that handles have been copied by the user and became
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; stale. We do not allow for render resource handle copying as this could result into a handle
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; pointing to an updated descriptor.
&lt;/span&gt;    ByteAddressBuffer versionBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP_UNIFORM&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandleUniform&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; g_bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;versionHeap&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Read &amp;amp; Write share same version.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; expectedVersion &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; versionBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; expectedTag &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;resourceTag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedVersionValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; expectedVersion &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;writable&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;isWritable&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passed_validation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; passedVersionValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; passedWritableValidation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Write out a small validation packet to a readback buffer
&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Finally return if the validation passed
&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; passed_validation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It should be noted that this technique is definitely not perfect, as by chance the version of an invalid handle &lt;em&gt;could&lt;/em&gt; match that of the current resource handle heap. However such a scenario would only be possible if all the following conditions are true:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The resource handle points to a valid resource of the same type&lt;/li&gt;
&lt;li&gt;The resource handle has exactly the same writability&lt;/li&gt;
&lt;li&gt;The resource handle has exactly the same looped version&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To improve the chances of this not happening, would mean that the size of a RenderResourceHandle would need to be increased to a size that could hold a larger version number. We however did not see this being worth the cost for what it provides.&lt;/p&gt;
&lt;h2 id=&quot;extending-the-mirror-heap&quot;&gt;Extending the mirror heap&lt;/h2&gt;
&lt;p&gt;We could also include additional metadata in the mirror heap, for example buffer sizes and texture dimensions. After verifying the bindless handles are valid, we can reliably perform buffer and texture bounds checking as well. Upon potential out of bounds access, we discard any load/writes and return a zero T value, avoiding any potential page faults.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;rwOffset&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RenderResourceHandle &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Make sure the version of the handle is the same as the reflected version from the CPU.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; If we have a mismatch here that means that handles have been copied by the user and became
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; stale. We do not allow for render resource handle copying as this could result into a handle
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; pointing to an updated descriptor.
&lt;/span&gt;    ByteAddressBuffer versionBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP_UNIFORM&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandleUniform&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; g_bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;versionHeap&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Read &amp;amp; Write share same version.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; expectedVersion &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; versionBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; expectedTag &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;resourceTag&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedVersionValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; expectedVersion &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;writable&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        passedWritableValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;isWritable&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; passed_validation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        passedTagValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; passedVersionValidation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; passedWritableValidation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;passed_validation&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Write out a small validation packet to a readback buffer if validation failed
&lt;/span&gt;      &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    ByteAddressBuffer boundsBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;ByteBufferHandle&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;bufferBoundsHeap&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Read buffer size
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; maxReadWriteBoundsInBytes &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; boundsBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;handle&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;readIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; isWithinBounds &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; rwOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; maxReadWriteBoundsInBytes&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;isWithinBounds&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Write a small validation packet to a readback buffer if validation failed
&lt;/span&gt;      &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Implementing bounds checking surfaced quite a bit of oversights and bugs that persisted in our rendering pipeline for some time.&lt;/p&gt;

&lt;h2 id=&quot;continuing-from-templated-bindless&quot;&gt;Continuing from templated bindless&lt;/h2&gt;
&lt;p&gt;When using the templated bindless abstraction that’s covered in &lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-templates-8920ca41326f&quot;&gt;part 2 of this series&lt;/a&gt;, we showed a way to abstract GPU resource access in order to recursively load any resource type from any piece of memory. Since we have this abstraction layer in place, we now have the ability to easily add debug logic within the abstraction for debug purposes.&lt;/p&gt;
&lt;p&gt;Secondly we create our macros that call the internal validation logic, this way we can easily toggle on/off validation at compile-time without having a lot of duplicate code.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#if&lt;/span&gt; SHADER_VALIDATION
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE_WITH_RETURN&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                               &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;writable&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; expectedTag&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;                                &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;                                                                                    &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE_WITH_RETURN_VALUE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;returnType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;             &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;writable&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; expectedTag&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;                                &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;returnType&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;                                                                      &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;                                           &lt;span class=&quot;syntax-punctuation syntax-separator syntax-continuation syntax-c&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;validateResourceInternal&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;writable&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; expectedTag&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; handle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE_WITH_RETURN&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE_WITH_RETURN_VALUE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;returnType&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-preprocessor syntax-c++&quot;&gt;VALIDATE_RESOURCE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;writable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;expectedTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-c++&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then call this macro in all of our abstracted function calls, while respecting the return type in case of a read function.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;RwRawBuffer&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    RenderResourceHandle handle&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-template syntax-c++&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;typename&lt;/span&gt; RWStructure&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-template syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; RWStructure &lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;loadWithOffset&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Macro ensure that if validation fails, target resource will not be read from.
&lt;/span&gt;        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;VALIDATE_RESOURCE_WITH_RETURN_VALUE&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kWritable&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kBufferResourceTag&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; RWStructure&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        RWByteAddressBuffer buffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;DESCRIPTOR_HEAP&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;RWByteBufferHandle&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-language syntax-c++&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;writeIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; buffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;Load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;RWStructure&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;offset&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the validation macro fails, we must make sure that target resource is not being used in order to avoid a potential pagefault, which inevitably leads to device removal. Additionaly if the condition fails, we write a small packet containing the validation failure information back to the CPU. Atomics are used here to ensure only a predetermined amount of messages can be written back to the CPU, to avoid flooding the CPU with millions of messages in case every thread in a shader fails validation.&lt;/p&gt;
&lt;h2 id=&quot;example-shader&quot;&gt;Example shader&lt;/h2&gt;
&lt;p&gt;Of course we want to ensure the bindless validation is always working as intended, this would require quite an extensive testing set to cover all of the potential resource types and validation cases, but for the sake of this blogpost, we will cover a small example shader that covers most basic validation cases.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-include syntax-c++&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-include syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;breda-render-backend-api::templated_bindless.hlsl&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c++&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;TEST_CASE_ALL_PASS&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;TEST_CASE_VERSION_FAILURE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;TEST_CASE_WRITABLE_FAILURE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;TEST_CASE_TAG_MISMATCH_FAILURE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;3&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c++&quot;&gt;Bindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    SimpleBuffer validBufferContainingInvalidHandle&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    Texture readOnlyTexture&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    RwTexture RwTextureOutput&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; threadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;SV_DispatchThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    Bindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;loadBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;Bindings&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; testCase &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; g_bindingsOffset&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;userData0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; sentryValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Test validation functionality
&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;testCase &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; TEST_CASE_ALL_PASS&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; All of the validation checks should pass.
&lt;/span&gt;        sentryValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;readOnlyTexture&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        sentryValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;validBufferContainingInvalidHandle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;RwTextureOutput&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; sentryValue&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;testCase &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; TEST_CASE_VERSION_FAILURE&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Test against invalid resource handle
&lt;/span&gt;        ArrayBuffer invalidBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;validBufferContainingInvalidHandle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;ArrayBuffer&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        sentryValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; invalidBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Ensure the value is actually consumed to avoid cases being compiled out.
&lt;/span&gt;        bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;RwTextureOutput&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; sentryValue&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;testCase &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; TEST_CASE_WRITABLE_FAILURE&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Test resource writability on a read-only texture.
&lt;/span&gt;        RwTexture invalidTexture &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;RwTexture&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;readOnlyTexture&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        invalidTexture&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; sentryValue&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;testCase &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; TEST_CASE_TAG_MISMATCH_FAILURE&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Test if resource tag matches.
&lt;/span&gt;        RwArrayBuffer invalidRwBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;RwArrayBuffer&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;RwTextureOutput&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        sentryValue &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; invalidRwBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Ensure the value is actually consumed to avoid cases being compiled out.
&lt;/span&gt;        bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;RwTextureOutput&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;store2D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;threadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; sentryValue&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we run the unit test with the test case set to “TEST_CASE_ALL_PASS”, the shader passes all validation checks. If we try to run the other test cases we now get an expected assert on the CPU side. These failures provide the user information on which pipeline failed validation, and some more metadata on what handle caused the failure case. Ideally we would like to print the resource names as well here, but that quickly becomes way too messy/complex to communicate back to the CPU.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/bindless-rendering-validation/01.png.webp&quot; alt=&quot;GPU validation error for TEST_CASE_VERSION_FAILURE&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;TEST_CASE_VERSION_FAILURE&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/bindless-rendering-validation/02.png.webp&quot; alt=&quot;GPU validation error for TEST_CASE_WRITABILITY_FAILURE&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;TEST_CASE_WRITABILITY_FAILURE&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/bindless-rendering-validation/03.png.webp&quot; alt=&quot;GPU validation error for TEST_CASE_TAG_MISMATCH_FAILURE&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;TEST_CASE_TAG_MISMATCH_FAILURE&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;All of this validation logic is inevitably going to cost a bit of performance, however this tends to be still relatively manageable. We took our shading kernel as a reference point which should be one of the more impacted kernels in terms of performance when having the bindless GPU validation enabled. This shading kernel jumped from ~250μs to approximately 450μs after enabling validation.&lt;/p&gt;
&lt;p&gt;Additionally we measured the total frametime difference with validation enabled vs disabled. This ended up increasing frametimes by quite a decent amount, no validation enabled gave an average frametime of 8.2ms while running with validation enabled increased this frametime to 13ms. Both Vulkan and Dx12 appear to be equally affected by this GPU validation layer.&lt;/p&gt;
&lt;p&gt;One of the big benefits of this validation system, is that GPU validation can be toggled on/off on a per-shader basis. Let’s say the rendering pipeline has a form of a TDR logger or something like DRED (in dx12), and a TDR occurs that points to specific pipeline, validation can be exclusively toggled within individual shaders of this pipeline. This would even allow for validating specific codeblocks within any shader.&lt;/p&gt;
&lt;p&gt;The validation techniques covered keep the rendering pipeline interactable while debugging potential issues, while allowing global or local shader validation, resulting in a relatively painless bindless debugging experience.&lt;/p&gt;
</description></item><item><title>Building pure Rust apps for Android</title><link>https://www.evolvebenchmark.com/blog-posts/building-pure-rust-apps-for-android</link><guid>https://www.evolvebenchmark.com/blog-posts/building-pure-rust-apps-for-android</guid><description>&lt;p&gt;&lt;em&gt;This blog post was written in 2022. It no longer reflects our state of the art for building Android apps with Rust.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Rust is an amazing language. It has allowed us at &lt;a href=&quot;https://github.com/Traverse-Research&quot;&gt;Traverse Research&lt;/a&gt; to develop with much greater speed and courage thanks to additional safety guarantees (bar the few &lt;em&gt;Foreign Function Interface&lt;/em&gt; crates that &lt;a href=&quot;https://github.com/Traverse-Research/opensource-ecosystem/#open-source&quot;&gt;we maintain&lt;/a&gt;!) and tooling, but does that hold true for Android applications as well?&lt;/p&gt;
&lt;p&gt;There are many ways to write Android apps. Not to mention the plethora of choices you get when also combining them with Rust code. A common use-case is embedding some Rust code (say a parser algorithm or networking infrastructure) inside an existing app. Assuming most apps are built in Android Studio and defined in &lt;code&gt;gradle&lt;/code&gt;, &lt;a href=&quot;https://github.com/mozilla/rust-android-gradle&quot;&gt;Mozilla’s rust-android-gradle plugin&lt;/a&gt; is the perfect choice for this scenario: after some configuration it compiles your Rust crate to a &lt;a href=&quot;https://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries&quot;&gt;&lt;code&gt;Shared Object&lt;/code&gt;&lt;/a&gt; that is subsequently embedded in an APK (installable Android app Package) and ready to be &lt;a href=&quot;https://docs.oracle.com/en/java/javase/18/docs/specs/jni/design.html#compiling-loading-and-linking-native-methods&quot;&gt;invoked from Java&lt;/a&gt; or Kotlin.&lt;/p&gt;
&lt;h2 id=&quot;1-app-compilation-internals&quot;&gt;1. App compilation internals&lt;/h2&gt;
&lt;p&gt;Going forward this story will cover pure Rust apps, which is how we build them at &lt;a href=&quot;https://github.com/Traverse-Research&quot;&gt;Traverse Research&lt;/a&gt;. Such apps contain no Java/Kotlin code and have to rely on whatever the Android system provides for so-called “native apps”.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that this story doesn’t intend to be a guide to using Rust on Android. Rather, it serves to discuss the various options available for building Android binaries and applications, discuss technical details of their implementations, lay out caveats and finally figure out where to go next.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let’s start by uncovering how a typical Android build environment is constructed, before diving into the (Rust!) tools that automate this for you.&lt;/p&gt;
&lt;p&gt;First and foremost is Rust’s &lt;a href=&quot;https://doc.rust-lang.org/rustc/platform-support.html#tier-2&quot;&gt;tier 2 compiler support&lt;/a&gt; for Android on popular architectures, most notably &lt;code&gt;aarch64&lt;/code&gt; and &lt;code&gt;arm(v7)&lt;/code&gt; as used on most mobile phones and &lt;code&gt;x86_64&lt;/code&gt;/&lt;code&gt;i686&lt;/code&gt; for desktop (emulator) usage. Without this you wouldn’t be able to compile Rust code for Android, but we unfortunately have to go beyond the Rust compiler and set up more.&lt;/p&gt;
&lt;p&gt;Enter &lt;a href=&quot;https://en.wikipedia.org/wiki/Linker_%28computing%29&quot;&gt;the linker&lt;/a&gt;: &lt;code&gt;cargo&lt;/code&gt; invokes the host linker which isn’t able to compile applications for Android and doesn’t typically have support for the architectures mentioned above. In a traditional setup one would download &lt;a href=&quot;https://developer.android.com/ndk/downloads&quot;&gt;Android’s official NDK&lt;/a&gt; [and utilize the binaries in there].&lt;/p&gt;
&lt;p&gt;Unfortunately most crates pull in a non-Rust dependency somewhere. Those dependencies compile C or C++ code ad-hoc (instead of bundling pre-compiled libraries for a limited set of architectures, platforms and optimization flags), and typically use &lt;a href=&quot;https://crates.io/crates/cc&quot;&gt;&lt;code&gt;cc-rs&lt;/code&gt;&lt;/a&gt; to make compiler invocation painless. This crate flawlessly supports &lt;a href=&quot;https://docs.rs/cc/latest/cc/#external-configuration-via-environment-variables&quot;&gt;per platform and architecture compiler configuration&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now that our Rust library is squared away, it is time to look at the remaining tools necessary to turn this into an installable Android app package. We start with &lt;a href=&quot;https://developer.android.com/studio/command-line/aapt2&quot;&gt;&lt;code&gt;aapt&lt;/code&gt;&lt;/a&gt; which is basically a glorified &lt;code&gt;zip&lt;/code&gt; compressor that inserts and compiles XML files to a binary representation, and packages up other assets and resources that constitute an APK. In here goes your &lt;code&gt;AndroidManifest.xml&lt;/code&gt; together with native libraries and (for “normal apps”) Java code.&lt;/p&gt;
&lt;p&gt;This package subsequently has to be &lt;a href=&quot;https://developer.android.com/studio/command-line/zipalign&quot;&gt;aligned&lt;/a&gt; and &lt;a href=&quot;https://developer.android.com/studio/command-line/apksigner&quot;&gt;signed&lt;/a&gt;. A debug key, in the form of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_KeyStore&quot;&gt;Java Keystore&lt;/a&gt; is generated using &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html&quot;&gt;&lt;code&gt;keytool&lt;/code&gt;&lt;/a&gt; and used to sign an application. Alternatively a release key can be provided via crate metadata.&lt;/p&gt;
&lt;p&gt;The resulting APK is now ready to be installed to an Android device of choice. During development &lt;a href=&quot;https://developer.android.com/studio/command-line/adb&quot;&gt;&lt;code&gt;adb&lt;/code&gt;&lt;/a&gt; (short for Android Debug Bridge) is typically used for quickly installing and relaunching an application, and it is the tool of choice to extract live logs from a running device.&lt;/p&gt;
&lt;p&gt;The same APK file can be distributed to clients, but can unfortunately no longer be published to Google&#39;s Play Store which requires a newer &lt;a href=&quot;https://developer.android.com/guide/app-bundle&quot;&gt;AAB format&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;2-rust-tools-for-compiling-native-apps&quot;&gt;2. Rust tools for compiling native apps&lt;/h2&gt;
&lt;p&gt;The Rust community provides multiple tools to build binaries and apps (APKs) for Android (disclaimer: I maintain and contribute to &lt;code&gt;cargo-apk&lt;/code&gt; and &lt;code&gt;xbuild&lt;/code&gt;). While different in approach and design, these tools have a few responsibilities in common:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Detect an Android NDK installation&lt;/li&gt;
&lt;li&gt;Provide its linker to &lt;code&gt;cargo&lt;/code&gt; for creating libraries and executables&lt;/li&gt;
&lt;li&gt;Provide its compiler(s) to &lt;a href=&quot;https://crates.io/crates/cc&quot;&gt;&lt;code&gt;cc-rs&lt;/code&gt;&lt;/a&gt; for crates that need to compile native (C/C++) code&lt;/li&gt;
&lt;li&gt;Make Android libraries available to the linker&lt;/li&gt;
&lt;li&gt;Invoke Rust’s compiler (generally through the &lt;code&gt;cargo&lt;/code&gt; build system) to compile the crate into an executable or shared object that runs on Android on the desired architecture&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And that’s “all there is” to compiling Rust crates to runnable binaries on Android, and what our first tool for today, &lt;a href=&quot;https://crates.io/crates/cargo-ndk&quot;&gt;cargo-ndk&lt;/a&gt;, provides. This tool compiles libraries and command-line executable for Android, allowing them to be embedded in applications or invoked from &lt;code&gt;adb shell&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That is only part of the effort to create an app for distribution and perhaps publishing on the Android App Store (Google Play), and where &lt;a href=&quot;https://crates.io/crates/cargo-apk&quot;&gt;&lt;code&gt;cargo-apk&lt;/code&gt;&lt;/a&gt; comes into play as part of the bigger &lt;a href=&quot;https://github.com/rust-mobile&quot;&gt;rust-mobile&lt;/a&gt; ecosystem. &lt;em&gt;We&#39;ll cover the contents of true Android apps in the next chapter.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cargo-apk&lt;/code&gt; does much the same as &lt;code&gt;cargo-ndk&lt;/code&gt;, and builds an APK on top. This requires various extra steps, typically performed through utilities from Android’s NDK and SDK:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Parse extra metadata about a package from &lt;a href=&quot;https://doc.rust-lang.org/cargo/reference/manifest.html&quot;&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;&lt;/a&gt; (Rust’s way of describing a crate to the build system)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.android.com/studio/command-line/aapt2&quot;&gt;&lt;code&gt;aapt&lt;/code&gt;&lt;/a&gt;: Package compiled Rust libraries and any other support libraries into an APK&lt;/li&gt;
&lt;li&gt;Generate &lt;a href=&quot;https://developer.android.com/guide/topics/manifest/manifest-intro&quot;&gt;&lt;code&gt;AndroidManifest.xml&lt;/code&gt;&lt;/a&gt; to explain to Android how to treat and expose the app to the user&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.android.com/studio/command-line/zipalign&quot;&gt;&lt;code&gt;zipalign&lt;/code&gt;&lt;/a&gt;: Align zip contents to make them mappable on Android&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html&quot;&gt;&lt;code&gt;keytool&lt;/code&gt;&lt;/a&gt;: Generate a &lt;code&gt;debug.keystore&lt;/code&gt; to sign the app, if it doesn’t exist yet&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.android.com/studio/command-line/apksigner&quot;&gt;&lt;code&gt;apksigner&lt;/code&gt;&lt;/a&gt;: Sign the application using &lt;code&gt;debug.keystore&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.android.com/studio/command-line/adb&quot;&gt;&lt;code&gt;adb&lt;/code&gt;&lt;/a&gt;: Install the generated APK on a connected phone or emulator&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This utility, while simple in design, has proven cumbersome to deploy and use. Its large set of dependencies on Android’s NDK and other SDK packages make it nontrivial to use &lt;a href=&quot;https://github.com/Traverse-Research/android-sdkmanager-rs/&quot;&gt;out-of-the-box&lt;/a&gt;, especially on Windows. Parsing crate metadata hasn’t been optimal or complete either. Recent efforts from &lt;a href=&quot;https://github.com/dvc94ch/&quot;&gt;David Craven&lt;/a&gt; resulted in &lt;a href=&quot;https://github.com/cloudpeers/xbuild/&quot;&gt;&lt;code&gt;xbuild&lt;/code&gt;&lt;/a&gt;, a build-tool that abstracts most of this setup away while relying on more standard tools:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Houses cross-compilation for multiple platforms in one utility and manifest format: it supports Windows, Android, MacOS and iOS&lt;/li&gt;
&lt;li&gt;Supports native Rust code as well as &lt;a href=&quot;https://flutter.dev/&quot;&gt;Flutter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;LLVM / clang include support for aforementioned platforms out of the box: by invoking the host compiler with a cross-compilation &lt;code&gt;--target&lt;/code&gt; it isn’t necessary to download an install a toolchain for every individual platform&lt;/li&gt;
&lt;li&gt;Repackages a slimmed-down NDK containing only libraries and headers&lt;/li&gt;
&lt;li&gt;Has built-in support for assets and icons&lt;/li&gt;
&lt;li&gt;Provides native implementations of &lt;code&gt;aapt&lt;/code&gt;, &lt;code&gt;keytool&lt;/code&gt;, &lt;code&gt;apksigner&lt;/code&gt;, and &lt;a href=&quot;https://github.com/cloudpeers/xbuild/pull/70&quot;&gt;soon &lt;code&gt;adb&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Also in progress: support for Android App Bundles&lt;/li&gt;
&lt;li&gt;Automatically follows and prints phone logs after installing the app through &lt;code&gt;x run --device adb:&amp;lt;id&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;And probably more that I have yet to discover 🥳&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The tool works well enough for our purposes, but still misses some critical infrastructure around library detection and packaging that’s currently being fleshed out, but the community goal is to eventually supersede and replace &lt;code&gt;cargo-apk&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A honourable mention goes out to &lt;a href=&quot;https://github.com/BrainiumLLC/cargo-mobile&quot;&gt;&lt;code&gt;cargo-mobile&lt;/code&gt;&lt;/a&gt;: this tool creates XCode and Android Studio projects that embed Rust crates. It is also actively maintained but different from the other approaches: we don’t use it and as such can’t comment on it.&lt;/p&gt;
&lt;p&gt;In the end these tools only bring us &lt;em&gt;so far&lt;/em&gt;. Our hopes and dreams are to &lt;a href=&quot;https://github.com/rust-lang/cargo/issues/10430&quot;&gt;embed these configuration and packaging stages directly into &lt;code&gt;cargo&lt;/code&gt;&lt;/a&gt;, or provide them as a &lt;code&gt;cargo&lt;/code&gt; extension. This rids us of fragile manifest parsing, incomplete commandline-argument hijacking and trying very hard to replicate every &lt;code&gt;cargo&lt;/code&gt; intricacy when it comes to parsing and handling targets in crates, but is a much bigger endeavour that requires carefully defining how and what &lt;code&gt;cargo&lt;/code&gt; communicates with such extensions. This expands beyond Android APK to support the likes of packages and platforms also provided by &lt;code&gt;xbuild&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-activity-lifecycle-and-ui&quot;&gt;3. Activity lifecycle and “UI”&lt;/h2&gt;
&lt;p&gt;Native Android apps don’t look like your average (commandline) executable, but will always be associated with a physical window (a so-called &lt;code&gt;Activity&lt;/code&gt; in Android terminology) on the screen. The Android framework contains a &lt;a href=&quot;https://developer.android.com/reference/android/app/NativeActivity&quot;&gt;&lt;code&gt;NativeActivity&lt;/code&gt;&lt;/a&gt; subclass of said &lt;code&gt;Activity&lt;/code&gt; that loads a native library in which your (Rust) code resides, allowing apps to be created without any Java code/classes whatsoever. It serves as a bridge between &lt;a href=&quot;https://developer.android.com/guide/components/activities/activity-lifecycle&quot;&gt;Android’s app lifecycle&lt;/a&gt; and native code, forwarding events through callbacks. This lifecycle details how an app starts and moves to back- and foreground while users navigate between apps on their phones.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://crates.io/crates/ndk-glue&quot;&gt;&lt;code&gt;ndk-glue&lt;/code&gt;&lt;/a&gt; from &lt;a href=&quot;https://github.com/rust-mobile&quot;&gt;&lt;code&gt;rust-mobile&lt;/code&gt;&lt;/a&gt; provides a Rust abstraction over this &lt;code&gt;NativeActivity&lt;/code&gt; and can be seen as a partial implementation of &lt;a href=&quot;https://developer.android.com/reference/games/game-activity/group/android-native-app-glue&quot;&gt;Android’s Native App Glue library&lt;/a&gt; in pure Rust. One of its consumers is &lt;a href=&quot;https://github.com/rust-windowing/winit&quot;&gt;&lt;code&gt;winit&lt;/code&gt;&lt;/a&gt;, a cross-platform window creation and management crate. It provides the user an event loop to process window and input events, and a place to redraw screen contents.&lt;/p&gt;
&lt;p&gt;In terms of “user interface”, &lt;code&gt;NativeActivity&lt;/code&gt; provides access to a &lt;a href=&quot;https://developer.android.com/reference/android/view/SurfaceView&quot;&gt;&lt;code&gt;SurfaceView&lt;/code&gt;&lt;/a&gt; (more specific: its &lt;a href=&quot;https://developer.android.com/reference/android/view/Surface&quot;&gt;&lt;code&gt;Surface&lt;/code&gt;&lt;/a&gt; / &lt;a href=&quot;https://docs.rs/ndk/latest/ndk/native_window/struct.NativeWindow.html&quot;&gt;&lt;code&gt;NativeWindow&lt;/code&gt;&lt;/a&gt; producer-counterpart) that spans the entire Activity “window”. One can &lt;a href=&quot;https://developer.android.com/ndk/reference/group/a-native-window#anativewindow_lock&quot;&gt;blit pixels to it directly&lt;/a&gt; or create an &lt;a href=&quot;https://github.com/rust-windowing/glutin/blob/4e55db7e65a7bbd08d32a5b26fd7827b4aaf4211/glutin/src/platform_impl/android/mod.rs#L35-L48&quot;&gt;OpenGL&lt;/a&gt; or &lt;a href=&quot;https://github.com/ash-rs/ash/blob/1459da47bce80fc022701809d56ac27af132ea5f/ash-window/src/lib.rs#L83-L86&quot;&gt;Vulkan&lt;/a&gt; surface and use the respective graphics API to draw to it from the GPU.&lt;/p&gt;
&lt;h2 id=&quot;4-improving-on-the-status-quo&quot;&gt;4. Improving on the status-quo&lt;/h2&gt;
&lt;p&gt;Without diving too much into the inner workings of &lt;code&gt;ndk-glue&lt;/code&gt;, it suffices to say that there &lt;a href=&quot;https://github.com/rust-windowing/winit/issues/2293&quot;&gt;are multiple active issues and attempts for improvement&lt;/a&gt; around this crate and the way it integrates with &lt;code&gt;winit&lt;/code&gt;. Some have recently been solved, others require more careful planning and ecosystem-wide migrations to newer systems.&lt;/p&gt;
&lt;p&gt;Firstly support for Android’s new &lt;a href=&quot;https://developer.android.com/games/agdk/integrate-game-activity&quot;&gt;&lt;code&gt;GameActivity&lt;/code&gt;&lt;/a&gt; class. Or rather, part of the &lt;a href=&quot;https://developer.android.com/games/agdk&quot;&gt;Android Game Development Kit&lt;/a&gt;. Unlike &lt;code&gt;NativeActivity&lt;/code&gt; this class is distributed as an &lt;a href=&quot;https://developer.android.com/studio/projects/android-library&quot;&gt;Android Archive (AAR)&lt;/a&gt; and must be bundled with the APK, containing all the support code that makes this Activity backwards-compatible down until SDK level 16 (Android 4.1).&lt;/p&gt;
&lt;p&gt;Binding said class and using it in &lt;code&gt;winit&lt;/code&gt; will come with a larger design shift for &lt;code&gt;ndk-glue&lt;/code&gt;. Most of its issues stem from the fact that it only provides a thin API on top of NDK primitives, and requires “downstream crates” like &lt;code&gt;winit&lt;/code&gt; to carefully follow its documentation (or as much as I’ve been &lt;a href=&quot;https://github.com/rust-mobile/ndk/commit/bfbb3b5642e0bf513f50424eb0a98d456756f118&quot;&gt;able to write&lt;/a&gt; over the &lt;a href=&quot;https://github.com/rust-mobile/ndk/commit/66eb8ca2a2f13713b97442c428c489cf28db6dc1&quot;&gt;past year&lt;/a&gt; or so) to keep things working; especially around &lt;a href=&quot;https://github.com/rust-mobile/ndk/pull/134&quot;&gt;object synchronization&lt;/a&gt;. Redesigning &lt;code&gt;ndk-glue&lt;/code&gt; and a &lt;code&gt;GameActivity&lt;/code&gt; wrapper to sit behind a shared API that makes them interchangeable will automatically address some of those issues.&lt;/p&gt;
&lt;p&gt;At the same time storage of “globals” in &lt;code&gt;static&lt;/code&gt; variables continue to be a pain-point. For one because having multiple versions of &lt;code&gt;ndk-glue&lt;/code&gt; (containing said &lt;code&gt;static&lt;/code&gt; globals) in a dependency graph results in the variables being duplicated: if a user initializes &lt;code&gt;ndk-glue 0.6&lt;/code&gt;, but &lt;code&gt;winit&lt;/code&gt; or &lt;a href=&quot;https://crates.io/crates/app_dirs2&quot;&gt;&lt;code&gt;app_dirs2&lt;/code&gt;&lt;/a&gt; subsequently tries to pull one of those values out of &lt;code&gt;ndk-glue 0.5&lt;/code&gt;, nothing will be available. We’ve solved this for some variables through a new &lt;a href=&quot;https://crates.io/crates/ndk-context&quot;&gt;&lt;code&gt;ndk-context&lt;/code&gt;&lt;/a&gt; crate that won’t ever see a breaking release version.&lt;/p&gt;
&lt;p&gt;However, there are important reasons for eliminating use of &lt;code&gt;static&lt;/code&gt;s altogether. As a refresher for those aware of Android’s architecture: an app can have multiple &lt;code&gt;Activity&lt;/code&gt; &lt;em&gt;instances&lt;/em&gt;, and they all run within the same process. This means that any global reference to a &lt;em&gt;single&lt;/em&gt; activity eventually end up referencing the wrong one. An API change has already been proposed that provides &lt;code&gt;NativeActivity&lt;/code&gt; handles in the “main” callback only, and put the burden on the user to pass it on to &lt;code&gt;winit&lt;/code&gt;, for example in a window constructor. This was found to be too disruptive of a change back in the day, but now seems to be the only way forward to defeat the issue.&lt;/p&gt;
&lt;p&gt;Finally there are concerns that neither &lt;code&gt;NativeActivity&lt;/code&gt; nor &lt;code&gt;GameActivity&lt;/code&gt; provide bindings for everything Android has to offer. &lt;code&gt;Intent&lt;/code&gt;s, &lt;code&gt;onNewIntent&lt;/code&gt; and &lt;code&gt;onActivityResult&lt;/code&gt; being the most commonly reported or asked about. &lt;a href=&quot;https://github.com/rust-mobile/android-intent&quot;&gt;It is possible to interop with &lt;code&gt;Intent&lt;/code&gt;s&lt;/a&gt; through &lt;a href=&quot;https://crates.io/crates/jni&quot;&gt;&lt;code&gt;JNI&lt;/code&gt;&lt;/a&gt;, yet it is not possible to insert the aforementioned callbacks into Activity classes unless they are subclassed, which requires some form of Java support in the build chain or precompiled Java classes provided by the crate.&lt;/p&gt;
&lt;p&gt;All in all there’s still a lot to do to make Rust apps on Android truly viable in a professional context. Our main desire is to embed at least the necessary tooling to compile Rust crates into APKs directly in &lt;code&gt;cargo&lt;/code&gt;. Stay tuned, and if you find this deeper dive in some of Android + Rust intricacies and issues interesting, feel free to reach and help out!&lt;/p&gt;
</description></item><item><title>Cooperative Vectors Introduction</title><link>https://www.evolvebenchmark.com/blog-posts/cooperative-vectors-introduction</link><guid>https://www.evolvebenchmark.com/blog-posts/cooperative-vectors-introduction</guid><description>&lt;h2 id=&quot;a-brief-history&quot;&gt;A brief history&lt;/h2&gt;
&lt;p&gt;In 2021 we started investigating use cases for Neural Networks (NN) in our rendering engine. Our first successful attempt was with &lt;a href=&quot;https://dl.acm.org/doi/10.1145/3550082.3564188&quot;&gt;Neural Materials (NM)&lt;/a&gt;.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1799pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/neuralmat2-1.jpeg.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Capture of NM implemented in our hybrid renderer. These materials were trained on data from &lt;a href=&quot;https://cg.cs.uni-bonn.de/btfdb&quot;&gt;UBO2014.&lt;/a&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Initially we only needed support for inference, since training of the NM was done &quot;offline&quot; in PyTorch. At the time, hardware accelerated inference was only supported through early vendor specific extensions on vulkan (Cooperative Matrix). Therefore, we built our own infrastructure for NN inference. This was built on top of our render graph, and fully in compute shaders (hlsl) without the use of any extension, to be able to deploy on all our target platforms and backends. One year down the line we saw impressive results from &lt;a href=&quot;https://research.nvidia.com/publication/2021-06_real-time-neural-radiance-caching-path-tracing&quot;&gt;Neural Radiance Caching (NRC)&lt;/a&gt;, which required runtime training of (mostly small, 16, 32 or 64 features wide) NNs. This led to the expansion of our framework to support inference and training pipelines.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1080px&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/evolve-nrc-10by2.webp.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Capture of NRC in the Evolve scene.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;While GPU’s had hardware dedicated to accelerate NN inference, there was not a way of accessing these features in shaders, especially cross-platform. For example, on desktop hardware we have different options per vendor: &lt;strong&gt;NVIDIA Tensor Cores&lt;/strong&gt;, at the time, were only accessible through CUDA, and &lt;strong&gt;Intel&lt;/strong&gt;&#39;s &lt;strong&gt;Xe Matrix Extensions (XMX)&lt;/strong&gt; were vendor-specific, while &lt;strong&gt;AMD&lt;/strong&gt; had &lt;strong&gt;Wave Matrix Multiply Accumulate (WMMA)&lt;/strong&gt; instructions that use the &quot;normal&quot; shader cores.&lt;/p&gt;
&lt;p&gt;As mentioned above, the Cooperative Matrix extension &lt;strong&gt;SPV_NV_cooperative_matrix&lt;/strong&gt;,&lt;strong&gt; &lt;/strong&gt;now elevated to &lt;strong&gt;KHR&lt;/strong&gt;, was introduced as an abstraction for (tiled) matrix-to-matrix operations with the ability to access hardware accelleration, if present. This extension added new shader instructions to interpret buffers as matrices and to multiply them together. Similarly, DirectX added a very similar feature called &lt;strong&gt;WaveMatrix&lt;/strong&gt;, initally intended for Shader Model (SM) 6.8, but then itnever released out of preview.&lt;/p&gt;
&lt;p&gt;As a result of research on &lt;a href=&quot;https://research.nvidia.com/labs/rtr/neural_appearance_models/&quot;&gt;Neural Materials&lt;/a&gt; and &lt;a href=&quot;https://research.nvidia.com/labs/rtr/neural_texture_compression/&quot;&gt;Neural Texture Compression (NTC) by NVIDIA&lt;/a&gt;, they introduced a feature in Optix and a Vulkan extension: Cooperative Vector &lt;strong&gt;VK_NV_cooperative_vector&lt;/strong&gt;. The solution proposed in the NTC paper compresses a set of textures for a material (like albedo, normal, roughness and metallic) into a NN and a learned texture-like representation. Both presented an interesting challenge: each material would have its own network, thus resulting in a situation where adjacent pixels on the screen might sample different textures, requiring an evaluation of a different network and therefore a different set of weights. This is not currently possible with Cooperative Matrix, which are meant for non-divergent work.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:976pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/coopvec-nm.png.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Example of divergent evaluation in NM, where 3 networks are needed to render the 3 materials.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Similarly NM, have the same issue, where different pixels might require different sets of weights. The way we solved it in our inital implementation was to bucket queries to the same materials and run multiple dispatches, one per material. This solution is not ideal, but works in practice, whilst being cumbersome and quite involved, ideally this should just be a branch in your shaders. Cooperative Vector solves this challenge by shifting interface from a matrix-matrix (in Cooperative Matrix) to a vector-matrix operation.&lt;/p&gt;
&lt;p&gt;A simpler situation that still benefits from Cooperative Vector is NRC, illustrated in the image below. For NRC each pixel will contain different input parameters, like the normal (N), view direction (V), roughness (R), albedo (A) and specular f0 (S). Differently than NM, the input will be fed into the same network, which won’t require different weights/matrices. This scenario is the best case scenario, performance wise, for Cooperative Vectors, since this will be scheduled like a matrix-matrix multiplication by composing the input matrix by the input vectors.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:976pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/coopvec-nrc-1.jpeg&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Example from NRC, where different input vectors for each &quot;pixel&quot; are provided, but are fed into a single network.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;what-is-a-cooperative-vector&quot;&gt;What is a Cooperative Vector?&lt;/h2&gt;
&lt;p&gt;Let&#39;s clarify some terminology first: a &lt;strong&gt;cooperative vector,&lt;/strong&gt; or &lt;strong&gt;long vector&lt;/strong&gt; in Dx/HLSL, is the data structure itself. To avoid confusion, from now onwards, we will use &lt;strong&gt;long vector&lt;/strong&gt;,&lt;strong&gt; &lt;/strong&gt;when referring to the data structure. On the other hand, we will use &lt;strong&gt;Cooperative Vector&lt;/strong&gt; to refer to the operations and hardware acceleration mechanisms that work with long vectors.&lt;/p&gt;
&lt;p&gt;The &quot;Cooperative&quot; in Cooperative Vector refers to an implementation detail of the hardware acceleration, where individual vector-matrix multiply requests submitted by threads in a wave are combined into a matrix-matrix operation accelerated collectively for the wave. This name doesn&#39;t appear in HLSL code itself, just vector types and operations like vector-matrix multiplication as shown in the examples below.&lt;/p&gt;
&lt;p&gt;So what are they exactly? Long vectors in HLSL are simply vector data structures that can be much longer than the usual 4 elements we&#39;ve been used to in shaders. Nothing more, nothing less. They&#39;re a &lt;strong&gt;transparent data structure&lt;/strong&gt; where the user can read and write individual elements via index, do operations on vectors like addition, multiplication, max (ReLU), etc.&lt;/p&gt;
&lt;p&gt;From the looks of it, the registers for these vectors are directly stored as &lt;strong&gt;VGPRs&lt;/strong&gt; (Vector General Purpose Registers), not in special registers even when hardware acceleration is available. From the perspective of a single lane (thread), the full long vector is stored in VGPR.&lt;/p&gt;
&lt;h3 id=&quot;thread-execution-model&quot;&gt;Thread / Execution Model&lt;/h3&gt;
&lt;p&gt;Now that we have an idea of what are Cooperative Vectors, where can they be used? At the moment, on both Vulkan and DirectX, they should be accessible in &lt;strong&gt;all shader stages&lt;/strong&gt;, although we mostly use them in compute shaders . That being said, since the idea is to provide a way to support divergent data; in pixel and compute shaders we will have each pixel/thread hold its own long vector.&lt;/p&gt;
&lt;p&gt;The most common way to implement it, and the most efficient way we found in practice, is to have each thread own its own long vector, although it is possible to store long vectors in shared memory. An important note: long vectors belong to the invocation (thread) they&#39;re declared in, or could be stored in shared memory, and don&#39;t require uniform control flow or fully occupied waves for functionality. On the other hand, uniform paths will enable driver fast paths for better performance, and features like Shader Execution Reordering (SER) could help with that.&lt;/p&gt;
&lt;h2 id=&quot;the-matrices&quot;&gt;The Matrices&lt;/h2&gt;
&lt;p&gt;Matrices come in two flavors:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Plain Layouts:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Row Major&lt;/li&gt;
&lt;li&gt;Column Major&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Optimal Layouts:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MulOptimal (Inference)&lt;/li&gt;
&lt;li&gt;OuterProductOptimal (Training/Accumulation)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;accessibility-in-shaders&quot;&gt;Accessibility in Shaders&lt;/h3&gt;
&lt;p&gt;Both Vulkan and DirectX provide interfaces that you can use to point to a memory address, like a buffer, and use it as a matrix.&lt;/p&gt;
&lt;p&gt;For &lt;strong&gt;plain layouts&lt;/strong&gt; (row/column major), we can address elements if we know the data type, matrix size, and layout. For &lt;strong&gt;optimal layouts&lt;/strong&gt;, however, the memory addresses are completely &lt;strong&gt;opaque&lt;/strong&gt;: we cannot do any read or write to a specific element, since the layout will be defined by the driver. Finally, the stride must be zero for optimal layouts, which prevents the user from accessing each element with indexing. This approach is an optimization similar to texture tiling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So here&#39;s the key difference: long vectors and matrices in plain layouts are transparent, while matrices in optimal layout are opaque.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The idea is that the user will fill up their long vector manually in the shader. For example, within a wave each thread will hold a long vector with different UV coordinate, view direction, normal, etc. These long vectors are then multiplied by fixed matrices that should not be modified directly in that same shader.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;inference-and-training-of-a-network&quot;&gt;Inference and Training of a Network&lt;/h2&gt;
&lt;p&gt;One of the simplest NN, the Multi Layer Perceptron (MLP), is built as a sequence of linear layers and activations. Each layer computation can be performed by a single matrix-matrix or vector-matrix operation and addition of a bias and finally an activation, like ReLU.&lt;/p&gt;
&lt;h3 id=&quot;inference&quot;&gt;Inference&lt;/h3&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:787pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/inference.png.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A single layer execution, &lt;em&gt;x&lt;/em&gt; is the input vector, &lt;em&gt;θ &lt;/em&gt;is the weight matrix, &lt;em&gt;b&lt;/em&gt; is the bias and &lt;em&gt;h&lt;/em&gt; is the result after the activation.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;For inference, we can use either row/column layout or MulOptimal (inference optimal) for our weight matrices. Obviously, in the majority of cases, you&#39;ll want MulOptimal layout as it should be faster. In our experiments, there was a significant difference even for small matrices.&lt;/p&gt;
&lt;p&gt;The way to fill out a MulOptimal matrix with the right data is to first write the data in row/column layout and then do a matrix conversion to optimal layout. Keep in mind that you need to query the destination size on the CPU before you allocate memory for the optimal layout matrix using &lt;strong&gt;ID3D12Device::GetLinearAlgebraMatrixConversionDestinationInfo&lt;/strong&gt;. This is because the size is implementation-dependent (vendor, driver, etc.) and might require more memory than you would expect. Once this is queried and the data allocated, we can perform the conversion using &lt;strong&gt;ID3D12GraphicsCommandList::ConvertLinearAlgebraMatrix&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This conversion is essentially just a copy operation that rearranges the data into the hardware&#39;s preferred format.&lt;/p&gt;
&lt;p&gt;Inference happens through &lt;strong&gt;MatMul&lt;/strong&gt; (or &lt;strong&gt;MatrixVectorMul&lt;/strong&gt;) or &lt;strong&gt;MatMulAdd&lt;/strong&gt;. The second takes another long vector (in DX) or a matrix (in Vulkan) as input for the bias.&lt;/p&gt;
&lt;h3 id=&quot;training&quot;&gt;Training&lt;/h3&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:907pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/backward.png.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Computing the gradients for the Network optimization.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Training a single layer involves computing three gradients: the gradient with respect to the input (∇x), the gradient with respect to the weights (∇θ), and (optionally) the gradient with respect to the bias (∇b). After computing the gradient of the loss wrt the layer output h (∇h), we can propagate it back and we can compute these using cooperative vectors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Gradient wrt input (∇x)&lt;/strong&gt;: This requires multiplying the incoming gradient ∇h by the transposed weight matrix θ&lt;sup&gt;T&lt;/sup&gt;. Fortunately, the &lt;strong&gt;MatMul&lt;/strong&gt; operation supports a transpose flag for MulOptimal (inference optimal) layouts, so we don&#39;t need to maintain a separate transposed copy of the weights.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gradient wrt weights (∇θ)&lt;/strong&gt;: This can be computed as an outer product between ∇h and the input x. This is where &lt;strong&gt;OuterProductAccumulate&lt;/strong&gt; comes in, accumulating the gradient across a batch, where each row in the batch is a cooperative vector. This can also be computed as a matrix multiply, which could also be more efficient in some scenarios, but we&#39;ll be focusing on using all the features provided by cooperative vectors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gradient wrt bias (∇b)&lt;/strong&gt;: If biases are present, their gradient is simply the accumulated ∇h across all samples, which can be done with cooperative vectors using &lt;strong&gt;VectorAccumulate&lt;/strong&gt;, this is also a simple atomic addition.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The other optimal layout, OuterProductOptimal (training or outer product optimal), does not require a conversion from row/column to optimal per se, as this will usually be used for storing the gradients of the weights. On the other hand, if we use this layout, we would likely end up converting our gradients from OuterProductOptimal to row/column major at some point.&lt;/p&gt;
&lt;p&gt;The OuterProductOptimal is used with the &lt;strong&gt;OuterProductAccumulate&lt;/strong&gt; function (or &lt;strong&gt;coopVecOuterProductAccumulateNV&lt;/strong&gt;in Vulkan). This takes two vectors and computes an outer product, which produces a matrix. This matrix is then accumulated into the target matrix, which MUST be in OuterProductOptimal layout. This operation is essentially a atomic addition/accumulation, where each element is atomically added to the corresponding element in the target matrix. Once this is done for all the batches in our training set, we can move on to copying the data with the conversion operation from OuterProductOptimal to a readable layout like row/column major.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:823pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/convert.png.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Matrix layout convertion pipeline.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;What we&#39;re left to do is to add our gradient multiplied by a learning rate to the previous weights (in row/column). Note that this matrix-to-matrix operation is not possible using cooperative vector intrinsics at the moment, &lt;strong&gt;support for matrix-matrix operations is planned for future releases in DirectX, while Vulkan has cooperative matrix for this (though at the moment it seems to not be compatible with the optimal layouts, nor Cooperative/long vectors)&lt;/strong&gt;. For now, you&#39;ll need to handle this on the CPU or with regular shader code. After this is moved to the row/column major layout, we can convert it back to MulOptimal to use during inference.&lt;/p&gt;
&lt;p&gt;And that is all!&lt;/p&gt;
&lt;blockquote&gt;Note that DirectX withdrew the Cooperative Vector proposal, and will instead land those features under a different name called &lt;a href=&quot;https://microsoft.github.io/hlsl-specs/proposals/0035-linalg-matrix/#example-usage-cooperative-vectors&quot;&gt;linear algebra&lt;/a&gt;, which combines Cooperative Vector and Cooperative Matrix. All that was discussed in this article should stay relevant even after Linear Algebra, although with a slightly different api and names. On the other hand, Vulkan promoted Cooperative Matrix to KHR, while Cooperative Vector is still a NVIDIA only extension.&lt;/blockquote&gt;</description></item><item><title>Dynamic levels of detail in Evolve</title><link>https://www.evolvebenchmark.com/blog-posts/dynamic-levels-of-detail-in-evolve</link><guid>https://www.evolvebenchmark.com/blog-posts/dynamic-levels-of-detail-in-evolve</guid><description>&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Hi everyone! In this blog post I will be detailing how we solved the problem of levels of detail for geometry and materials in our graphics framework Breda, which was needed for our next-gen benchmark Evolve. For this post I will focus on the more traditional solution to LODs that we went with, but we have also been experimenting with systems such as Nanite which you can read about &lt;a href=&quot;https://blog.traverseresearch.nl/creating-a-directed-acyclic-graph-from-a-mesh-1329e57286e5&quot;&gt;here&lt;/a&gt;. Code samples are in Rust and HLSL.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-04.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Exaggerated LOD transitions shown by wire-frame in the Evolve jungle scene.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h2 id=&quot;why-do-we-need-lods&quot;&gt;Why do we need LODs?&lt;/h2&gt;
&lt;p&gt;Evolve is a next-gen benchmark capable of measuring various aspects of your GPU to indicate how well it will perform in modern game-like workloads. As such, we need to closely mimic what games are doing and LODs are generally present in most engines in one form or another.&lt;/p&gt;
&lt;p&gt;Another reason for LODs is the increased scene complexity in Evolve, which caused us to exceed our polygon budget for mobile hardware. LODs helped reduce the triangle count to meet our performance target for both ray-tracing and rasterization.&lt;/p&gt;
&lt;p&gt;We also faced performance issues on mobile when most materials in the scene had a specular layer, which in turn cause more rays to be traced and expensive ReSTIR Reflections passes to run. While expensive, the rough reflections at a distance were barely noticeable on low upscaled resolutions. LODs offered a solution here, as we could still keep the reflective layers when close to a mesh, while falling back to the cheaper to evaluate materials from afar.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-4.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Tweaking the LOD selection bias at runtime. Some plants disappear entirely in their lowest LOD. Not all meshes have a LOD. At runtime, this is invisible.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h1&gt;The Asset Pipeline&lt;/h1&gt;
&lt;p&gt;The system starts offline when we define meshes in our workspace in various &lt;em&gt;yaml&lt;/em&gt; files. Here we can specify the source assets for each LOD, as well as how much of the screen should be covered by this mesh before transitioning. These files are automatically generated by a python script that exports our scenes from the editor as separate GLTF files.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-string syntax-unquoted syntax-plain syntax-out syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-tag syntax-yaml&quot;&gt;scenes&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-key-value syntax-mapping syntax-yaml&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;syntax-string syntax-unquoted syntax-plain syntax-out syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-tag syntax-yaml&quot;&gt;mesh1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-key-value syntax-mapping syntax-yaml&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;syntax-string syntax-unquoted syntax-plain syntax-out syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-tag syntax-yaml&quot;&gt;filename&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-key-value syntax-mapping syntax-yaml&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;breda-storage://models/mesh1_LOD0.gltf&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-string syntax-unquoted syntax-plain syntax-out syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-tag syntax-yaml&quot;&gt;lods&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-key-value syntax-mapping syntax-yaml&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-flow-sequence syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-sequence syntax-begin syntax-yaml&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;breda-storage://models/mesh1_LOD0.gltf&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-sequence syntax-yaml&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;breda-storage://models/mesh1_LOD1.gltf&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-sequence syntax-yaml&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;breda-storage://models/mesh1_LOD2.gltf&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-yaml&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-sequence syntax-end syntax-yaml&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-string syntax-unquoted syntax-plain syntax-out syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-tag syntax-yaml&quot;&gt;lod_activation_screen_sizes&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-key-value syntax-mapping syntax-yaml&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-flow-sequence syntax-yaml&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-sequence syntax-begin syntax-yaml&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-yaml&quot;&gt;1.000000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-sequence syntax-yaml&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-yaml&quot;&gt;0.800000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-sequence syntax-yaml&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-yaml&quot;&gt;0.400000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-sequence syntax-end syntax-yaml&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The source assets are loaded into our asset building pipeline, where we convert them into &lt;em&gt;MeshAssets&lt;/em&gt; which we cache. During this process, we build a single vertex and index buffer from all source LODs of a mesh. We also store metadata such as how many LODs there are and how many &lt;em&gt;Mesh Parts&lt;/em&gt; there are per LOD. A &lt;em&gt;Mesh Part&lt;/em&gt; in this case is a sub-set of vertices covered by a single material, defined by a range in the index buffer. Materials are mapped to their mesh part, and stored as a &lt;em&gt;MaterialBatchAsset&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;During this process, we automatically de-duplicate assets by checking their deterministic hashes. For example, two GLTF files specifying the same texture will result in only a single texture being processed and used at runtime.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/asset-diagram.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Diagram of our asset pipeline with regards to LODs.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;During development, I added automatic LOD generation using an existing Rust crate that culls the index buffer without touching the vertices. This was a quick solution for early testing and profiling while the final assets was not yet ready, but did not result in high quality LODs suitable for release.&lt;/p&gt;
&lt;h1&gt;Runtime&lt;/h1&gt;
&lt;h2 id=&quot;data-access&quot;&gt;Data Access&lt;/h2&gt;
&lt;p&gt;At runtime, we load the &lt;em&gt;MeshAssets&lt;/em&gt; from disk. The next step is to make the data available on the GPU. Fortunately we use bindless rendering, which greatly simplifies this step.&lt;/p&gt;
&lt;p&gt;We upload the index and vertex buffers to the GPU as separate buffer objects per mesh, and upload the mesh parts to a persistent GPU buffer that contains the mesh parts of all meshes. I wrote a simple freelist-allocator to manage the memory allocations in that buffer automatically when meshes are streamed in or out at runtime.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;When a mesh is streamed in this way, the offset in the mesh parts buffer for this mesh is returned. This offset allows us to dynamically index the right geometry in our shaders. We also remember the offset where each LOD starts within the mesh parts buffer (as seen in the diagram in the previous section).&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Materials follow the same pattern. We upload them to a monolithic buffer that maps 1 to 1 with the mesh parts buffer. That way if we know the index of the geometry, we also automatically know the index of the material.&lt;/p&gt;
&lt;p&gt;The code snippet below shows what a mesh part looks like, and the bookkeeping data that keep around for each mesh on the CPU.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; A mesh part is just an offset and size in the index buffer.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c&quot;&gt;MeshPart&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
  index_buffer_offset&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; u32&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
  num_indices&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; u32&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; CPU sided tracking of data for a mesh.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c&quot;&gt;CpuMeshData&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
  vertex_buffer&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; Arc&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;dyn Buffer&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
  index_buffer&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; Arc&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;dyn Buffer&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
  mesh_part_buffer_offset&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; u32&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;

  num_mesh_parts_per_lod&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; Vec&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;u32&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
  mesh_part_offsets_per_lod&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; Vec&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;u32&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
  mesh_parts&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; Vec&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;MeshPart&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we do some bookkeeping by creating buffers that map the unique identifier &lt;em&gt;(Node Handle; a 32-bit index&lt;/em&gt;) for each mesh instance in the scene to their vertex buffer, chosen LOD and mesh parts. On the shader side we can index into these buffers through their binding names &lt;em&gt;Material Bindings&lt;/em&gt;  and &lt;em&gt;Mesh Bindings&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I will explain later how we choose the LODs, but for now just assume that for each node in the scene that has a mesh attached, we have chosen the appropriate LOD.&lt;/p&gt;
&lt;p&gt;This is what indexing looks like in a pixel shader (pseudo-code). Assume that the &lt;em&gt;nodeHandle&lt;/em&gt; and &lt;em&gt;meshPartIndex (implicitly for a specific LOD)&lt;/em&gt; are known at this point. They are passed into graphics passes trough push constants or dynamically loaded for indirect draws, and for ray-tracing shaders we can query them from the ray hit results (instance index and geometry index).&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c&quot;&gt;Bindings&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c&quot;&gt;GpuMeshData&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    RawBuffer indexBuffer&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    RawBuffer positionsBuffer&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;MainPS&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; triangleId &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_PrimitiveID&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Our bindless entrypoint.
&lt;/span&gt;  Bindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; loadBindings&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;Bindings&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The index of the mesh part we&amp;#39;re rasterizing, can be a push constant.
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This mesh part already is of a specific LOD.
&lt;/span&gt;  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; meshPartIndex &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The handle of the instance of the mesh we&amp;#39;re drawing, push constant.
&lt;/span&gt;  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; nodeHandle &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Materials map 1:1 with mesh parts, so we can load it straight away!
&lt;/span&gt;  Material material &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;materialBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMaterial&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;meshPartIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We keep a mapping between mesh instance and mesh data buffer index.
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This gives us access to the index buffer and vertex buffer.
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; In a vertex shader, we could load the index and vertex.
&lt;/span&gt;  GpuMeshData meshData &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;meshBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMeshData&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;nodeHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; In rasterization we know the mesh part we&amp;#39;re drawing,
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We can index into a buffer to retrieve the index offset and size.
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; By this point, we can load the triangle specific data.
&lt;/span&gt;  MeshPart meshPart &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;meshBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMeshPart&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;globalMeshPartIdx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  Triangle tri &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; meshData&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadTriangle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;meshPart&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; bary&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; triangleId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  float4x4 transform &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;transformBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;nodeHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-c&quot;&gt;Bindings&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;shader&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;quot;&lt;/span&gt;anyhit&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;anyHitEntry&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;inout PayLoad payload
                                    &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_RayPayload&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; in Attributes attribs&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
    Bindings bnd &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; loadBindings&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;Bindings&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    float3 barycentrics &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; attribs&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;barycentrics&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; triangleIndex &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;PrimitiveIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The unique ID for each BLAS instance is equal to our node handle.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We enforce this when creating new BLAS instances.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; nodeHandle &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;InstanceID&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; And mesh parts map to the geometry index within the BLAS.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Note that this is local to the mesh; we have to add the LOD offset.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; localMeshPartIdx &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;GeometryIndex&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;  
    
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Add the global mesh part buffer offset for the chosen LOD.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We have chosen the LOD for this instance in another shader.
&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The bookkeeping happens there as well.
&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; lodMeshPartOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;meshPartOffsetForCurrentLod&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;nodeHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; globalMeshPartIdx &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; lodMeshPartOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; localMeshPartIdx&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;


    MeshPart meshPart &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;meshBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMeshPart&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;globalMeshPartIdx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    MeshData meshData &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;meshBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMeshData&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;nodeHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    Material material &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;materialBindings&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;loadMaterial&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;globalMeshPartIdx&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While these code snippets don&#39;t give all he details, they should be enough to get an understanding of how all data becomes accessible on the GPU side and how LOD levels are simply embedded in the indexing.&lt;/p&gt;
&lt;h2 id=&quot;lod-designation&quot;&gt;LOD Designation&lt;/h2&gt;
&lt;p&gt;Now that we have all the geometry uploaded to the GPU with an easy way to access it from any shader, we still have to choose which LOD we want to use. We base the LOD solely on how large the mesh is, and how far it is from the camera. The reason for this is because with ray tracing, even objects behind the camera need to appear sharp for reflections and shadows.&lt;/p&gt;
&lt;p&gt;The whole process happens in a single compute pass on the GPU. We have two good reasons why we chose to make this process GPU driven:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We update transforms and animate geometry on the GPU. That means we don’t have up-to-date position information of all instances on the CPU side, which is quite important for choosing a LOD.&lt;/li&gt;
&lt;li&gt;Draw call sorting on the CPU is expensive already, and adding in LODs would make it even more so. Instead of worsening this CPU bottleneck, we chose to move away from CPU sided draw call sorting altogether and use indirect drawing.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The LOD selection process looks something like this. We calculate and cache the AABB of all meshes vertex buffers in the scene. For vertex animated geometry this will require an extra pass to recalculate the AABB. We then estimate the solid angle of the view frustum on the camera unit sphere in steradians to determine which value constitutes as covering the entire screen. This can be done by using great circles and Girard’s theorem. The calculation happens in local space, and can therefore be greatly simplified.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;let frustum_spherical_angle &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;half_fov_radians&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;half_fov_radians&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
let screen_solid_angle &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;4f32 &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; frustum_spherical_angle&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; 2f32 &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; PI&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code snippet is a greatly simplified calculation of the solid angle of the view frustum on the camera unit sphere. The frustum is symmetrical and originates at the center of the camera unit sphere, so we only need to calculate a single spherical angle, because it is the same for each corner of the view frustum.&lt;/p&gt;
&lt;p&gt;A more in-depth explanation of how this works: Because the solid angle stays the same regardless of view direction,we assume tangent space. The frustum planes of the camera go through the camera position (the unit sphere center). This means they form great circles on the camera unit sphere. The angles between the frustum planes are given by their normals. Because of tangent space, the normal of the plane aligns with at least one axis. Two planes have an X and Z component, the other two a Y and Z. Since the angle is equal to the acos of the dot product, we can ignore the Y and X components (since they are multiplied with 0). This means that the only relevant component of the normal is Z. The Z component of the frustum plane itself then maps to the X component on a circle with the same rotation (cos(theta)). The Z component of the normal of that frustum plane then maps to the X component of the point rotated 90 degrees counter-clockwise (-sin(theta)). This means we can construct the normal of these planes by simply taking the -sin of their rotation angle. Because all planes are constructed in the same local space, we need to account for one of the planes starting with a 90 degree offset. This is equal to negating the -sin(theta) to be sin(theta).&lt;/p&gt;
&lt;p&gt;Doing this gives the spherical angle between two planes in our view frustum. Following Girard&#39;s theorem, we can then calculate the spherical excess which is equal to the solid angle in steradians.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;Finally, We project each mesh’s AABB onto the unit sphere around the camera to get their solid angle inside a compute pass. The solid angle is then divided by the view frustum&#39;s solid angle, so that we get an estimation of how much of the screen could be covered by this object if we looked at it head-on. This approximation of potential screen coverage lets us choose a LOD based on the coverage values defined for each mesh in our asset pipeline.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/camera-projection.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;How we project AABBs onto the camera unit sphere to estimate the required LOD.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;Aabb meshAabb &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;nodeBoundingBoxes&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt;Aabb&lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;nodeHandle&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; World to local of the mesh instance.
&lt;/span&gt;float4x4 worldToLocal &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;inverse&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;worldTransforms&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;nodeHandle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The camera position, relative to the mesh in local space.
&lt;/span&gt;float3 cameraLocal &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;mul&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;cameraPosition&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
                                                    worldToLocal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;xyz&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Project the AABB onto the camera position. 
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Returns the solid angle in steradians.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; solidAngle &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;aabbSolidAngle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;meshAabb&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; meshAabb&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; cameraLocal&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We have a tweakable scalar to bias towards picking higher
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; or lower quality LODs by pretending meshes are either closer or farther.
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Distance and solid angle of course scale by the inverse square law.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; lodDistanceScale &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;lodDistanceScale&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; inverseSquareScalar &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;lodDistanceScale &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; lodDistanceScale&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
solidAngle &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; inverseSquareScalar&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Ratio of projected mesh to the solid angle of the screen.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; screenSize &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; solidAngle &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; bnd&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;screenSolidAngle&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If interested, here’s a code sample of the &lt;em&gt;quadSolidAngle&lt;/em&gt; calculation. It takes the closest three AABB faces to the camera, and projects their four corners onto the camera unit sphere. It then applies Girard’s theorem to calculate the area of the spherical rectangle each face forms before summing the total area.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Calculate the integrated area of a polygon projected on a unit sphere,
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; in steradians.
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The vertices are normalized directions from the center of the sphere
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; to the corners of the polygon. This is based on Girard&amp;#39;s theorem.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;quadSolidAngle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;float3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;verts&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; sum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Calculate the normal for the plane on which each great circle lies.
&lt;/span&gt;  float3 normal1 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  float3 normal2 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  float3 normal3 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  float3 normal4 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; According to Girard&amp;#39;s theorem, the area of a polygon projected on a 
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; unit sphere is equal to the sum of its inner angles
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; minus (kPi * (N - 2)) where N is the number of vertices in the polygon.
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Negate the 2nd component, because the normals are all pointing inwards.
&lt;/span&gt;  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; dot0 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;normal1&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;normal2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; dot1 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;normal2&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;normal3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; dot2 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;normal3&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;normal4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; dot3 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;normal4&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;normal1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; sphericalExcess &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dot0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dot1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dot2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dot3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  sphericalExcess &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kPi&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; sphericalExcess&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Returns the float closest to the base.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;minDistance&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; diffA &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;base &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; a&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; diffB &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;base &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; b&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;diffA &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; diffB&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; b&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; a&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Calculate the area of an AABB projected on the unit sphere around a
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; position. The returned area is the solid angle expressed in steradians.
&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;aabbSolidAngle&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;float3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;minBounds&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; float3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;maxBounds&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; float3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;eye&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; If eye lies in the bounding box, then the entire sphere is covered.
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;eye &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; minBounds&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;eye &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; maxBounds&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;4.0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kPi&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Calculate the edges of the visible 3 faces.
&lt;/span&gt;  float3 closest &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;minDistance&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
                          &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;minDistance&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
                          &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;minDistance&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; areaSum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  float3 verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Closest face on the x-axis.
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      areaSum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;quadSolidAngle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Closest face on the y-axis.
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      areaSum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;quadSolidAngle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Closest face on the z-axis.
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; eye&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      verts&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;float3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;maxBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; minBounds&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; closest&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;z&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; eye&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
      areaSum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;quadSolidAngle&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;verts&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Due to floating point precision problems, the area can be very slightly
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; negative when at 0-scale.
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; areaSum&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;runtime-lod-tweaking&quot;&gt;Runtime LOD Tweaking&lt;/h2&gt;
&lt;p&gt;As you may recall from the first chapter, we specify per mesh when we want the LODs to transition by providing a list of screen-sizes. If we specify &quot;1&quot;, it implies we want to transition to this LOD only when the entire view is covered by that mesh. Likewise, &quot;0&quot; implies we want to use that LOD even if it is so tiny it’s not even visible. These values are calculated for us by the scene editing software we use. We upload them to the GPU so that our LOD designation shader can find the best matching LOD based on the AABB projection to view frustum projection ratio.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;lod_activation_screen_sizes&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1.000000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.800000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.400000&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you could see in the code samples earlier, there’s also a runtime scalar that we can use to fake objects being closer or further away using the inverse square law. Higher values will cause meshes to fall back to lower LODs more quickly, which is very useful when rendering at lower resolution devices such as phones. It gives us a knob to tweak all LOD transitions until we are happy in terms of performance and visual quality.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;lodDistanceScale&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;bnd&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-property syntax-object syntax-js&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-property syntax-object syntax-js&quot;&gt;lodDistanceScale&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-statement syntax-js&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;inverseSquareScalar&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-js&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-js&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;lodDistanceScale&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-js&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;lodDistanceScale&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-statement syntax-js&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;solidAngle&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-js&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;inverseSquareScalar&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-statement syntax-js&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other than that, I added some debug UI tools to allow us to manually lock in a specific LOD on the selected mesh.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-13.gif.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Our runtime debug editor to quickly visualize LODs in action.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;rasterization&quot;&gt;Rasterization&lt;/h2&gt;
&lt;p&gt;The impact of having LODs on our rasterization passes is that we had to change to indirect drawing. As I mentioned before, sorting draw calls was a big bottleneck on the CPU, so this was a good opportunity to switch.&lt;/p&gt;
&lt;p&gt;Now we simply dispatch a draw call for each mesh part (including the LODed ones), and set the actual draw count after we run our GPU based culling passes. We can then use the shader logic I showed before to access the geometry during drawing.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-video&quot; style=&quot;padding-bottom:34.78260869565217%&quot;&gt;&lt;div&gt;&lt;iframe allowfullscreen=&quot;true&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;https://player.vimeo.com/video/1069180714&quot; title=&quot;Evolve dynamic level of detail selection&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h2 id=&quot;ray-tracing&quot;&gt;Ray Tracing&lt;/h2&gt;
&lt;p&gt;Here I’ll explain how we do our TLAS and BLAS building each frame, and how it interacts with LODs. Because each LOD has unique geometry, we need a separate BLAS for each one.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Our compute shader to select a LOD for every mesh instance runs.&lt;/li&gt;
&lt;li&gt;We go over all BLAS instances in the scene, and swap their BLAS pointer with the one for the currently selected LOD. This happens in a compute shader as well.&lt;/li&gt;
&lt;li&gt;We build the new TLAS from all the BLAS instances.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As you can see, this is problematic because we have to ensure all BLASes have been built or refitted before the TLAS build happens, but we don’t know which LODs have been picked on the CPU side. This could be solved by using indirect BLAS builds and refits, but at the time of writing there is no wide API support for this feature yet. We are stuck to doing CPU sided BLAS builds for now. So instead of doing a single build, we’ll now have to do one for each LOD as well.&lt;/p&gt;
&lt;p&gt;We could add a heuristic and read-back with delay to make sure we can only pick BLASes that have been built so that we can reduce the amount of builds and refits per frame, but we chose not to solve this problem for the time being because of a few reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We know that 90% of our geometry is static. The BLAS only needs to be built once at startup, so we just do that for every LOD and call it a day. Startup time is a bit slower, but we can live with that.&lt;/li&gt;
&lt;li&gt;For animated geometry, it would quickly become expensive to build a BLAS for each LOD level, but since vertex animated meshes (The player character and dinosaurs in the case of Evolve) are the focus of attention, usually we’d want them to be high quality anyways. We chose to not give them any LODs and instead rely on occlusion culling to reduce the raster performance impact when they are off-screen.&lt;/li&gt;
&lt;li&gt;We have many wind-animated plants in Evolve which use LODs. This may seem like a worst-case scenario at first, but we also use a wind pooling system so that we can reuse the same animated plant in more than one location. Because of this, most plants appear in many places and would likely have most LOD levels active at all times. In that case we would still have to rebuild all BLASes for the plants anyways, so there’s nothing to gain here.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We played around with having more than a single TLAS as well, so that we could have low level LODs in one for systems such as GI for cheaper tracing where the high frequency details don’t matter as much. In the end we abandoned this idea because the discrepancy in geometry caused severe darkening and artifacts in some areas. The cost of building an extra TLAS was also too large for any gains to be worth it on mobile hardware.&lt;/p&gt;
&lt;h1&gt;Results&lt;/h1&gt;
&lt;p&gt;The overhead at runtime of this LOD system is very small. The only real additional cost is the LOD designation shader, which runs quite fast on an AMD RX 7900 XTX.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-20.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Our LOD designation shader running in just under 10 microseconds.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The BLAS refitting cost also isn’t terrible, despite refitting animated meshes and all their LODs each frame.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-21.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;The BLAS instance handle update before building our TLAS coming in at roughly 18 microseconds.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Our VBuffer timings also show an improvement when we enable LODing.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-22.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;VBuffer timings before applying LODs.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/dynamic-levels-of-detail-in-evolve-figure-23.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;VBuffer timings after LODs.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Keep in mind that these timings were captured on a modern GPU. The difference on mobile devices is much larger with a bigger impact on total frame times. Most importantly, the system gives us a tool to tweak performance if the need arises. By far not all of our meshes use LODs yet, so there is still more to be gained.&lt;/p&gt;
&lt;h1&gt;Final Thoughts&lt;/h1&gt;
&lt;p&gt;There are a few problems with our implementation, which I’ll briefly list here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No indirect BLAS refitting and rebuilding will likely form a new bottleneck in the future when we want to have animated LODs. Adding a slight delay in LOD selection and falling back to a “safe” one should be a good-enough solution until we get indirect refits and rebuilds.&lt;/li&gt;
&lt;li&gt;Shadow popping! A shadow can be right in front of the camera while its caster is not. If the LOD of the mesh switches, then you will see this happen. This depends a lot on the scene and light directions too, so we have been able to avoid the issue. In a fully dynamic world this is something to keep in mind that should be solved.&lt;/li&gt;
&lt;li&gt;We don’t have any in-engine solution to creating high quality LODs, and thus require an artist or external tool to do this work for us. Moving over to a Nanite-like system is definitely something that is interesting for the future to automate the workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the end of the journey, we have a LODing system that works well without being intrusive in the rest of our workflow or rendering pipelines. The interface for binding meshes and accessing them on the GPU has not changed at all, so integration with existing systems was easy.&lt;/p&gt;
&lt;p&gt;Most people don’t even know we have LODs, which means they work!&lt;/p&gt;
&lt;p&gt;I hope this post has been informational. As always, feel free to reach out if you have any questions.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
</description></item><item><title>Fast CDF generation on the GPU for light picking</title><link>https://www.evolvebenchmark.com/blog-posts/fast-cdf-generation-on-the-gpu-for-light-picking</link><guid>https://www.evolvebenchmark.com/blog-posts/fast-cdf-generation-on-the-gpu-for-light-picking</guid><description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In this blog post, I’ll take a deep dive into how we build our CDF for light sampling on the GPU at Traverse Research. I’ll give a quick rundown of what everything is, why you need it and finally how we made it run fast on the GPU.&lt;/p&gt;
&lt;p&gt;The first few sections are an introduction to concepts that may not be clear to everyone, so feel free to skip ahead to the part where I dive into optimizing the GPU algorithm if you already have a good understanding of CDFs!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/01.png.webp&quot; alt=&quot;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-is-a-cdf&quot;&gt;What is a CDF?&lt;/h2&gt;
&lt;p&gt;CDF stands for “cumulative distribution function”. In probability theory, it is a function that can tell you the probability that a random variable has taken on a certain value. This is done by summing the value of the probability density function (PDF) for each value that the random variable can assume.&lt;/p&gt;
&lt;p&gt;The below diagram might make it a bit clearer if I’ve confused you up until this point: The red line is the value of the CDF for a random variable. The random variable can take on the values of A, B, C and D (X-axis), and the Y axis represents the value of the PDF for each of those values.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/02.png.webp&quot; alt=&quot;CDF of a random variable with values A, B, C and D&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Personally I prefer to look at the CDF the way it is represented in code: an inclusive prefix sum array, where each element is the sum of the value of all elements that came before it (including its own value). The below diagram shows the PDF and CDF for the four values our random variable can assume in array form:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/03.png.webp&quot; alt=&quot;PDF and CDF in array form&quot; /&gt;&lt;/p&gt;
&lt;p&gt;If you are interested in reading more about probability theory, then I recommend &lt;a href=&quot;https://jacco.ompf2.com/2019/12/11/probability-theory-for-physically-based-rendering/&quot;&gt;this blog post&lt;/a&gt; by Jacco Bikker.&lt;/p&gt;
&lt;h2 id=&quot;where-and-how-are-cdfs-used&quot;&gt;Where and how are CDFs used?&lt;/h2&gt;
&lt;p&gt;With all that out of the way, you may be wondering why you would need a CDF. Well, it turns out that they are great when you want to stochastically select an element from a set proportional to its weight, and relative to the weight of all other elements in the set.&lt;/p&gt;
&lt;p&gt;This is possible, because a useful property of CDFs is that you can invert them: rather than querying the CDF what value corresponds to a known element, you randomly generate a CDF value and find which element corresponds to it. That way you can select your samples with a priority for the important ones (the ones where the PDF gives a higher value).&lt;/p&gt;
&lt;p&gt;A common scenario where this occurs is any algorithm that relies on monte carlo integration. If your dataset is too big to fully consider each iteration, then integrating over a few stochastic samples each cycle will eventually also converge to the correct result. By not taking random samples, but stochastically selecting based on weights you will converge to the right result faster. The stronger the relation between the importance of an element and its weight, the faster it will converge.&lt;/p&gt;
&lt;p&gt;There are many algorithms where this applies, but for the rest of this blog I will be focusing on our use case: sampling lights in a real time ray tracer.&lt;/p&gt;
&lt;h2 id=&quot;light-sampling-algorithm&quot;&gt;Light sampling algorithm&lt;/h2&gt;
&lt;p&gt;Let’s translate all that previous information to our use-case. We are running a real-time ray tracer with a scene that contains many lights. For every pixel, we want to know per light how much it contributes to the irradiance. In our actual implementation, we use monte carlo integration and we feed our selected light samples into a ReSTIR implementation, but for simplicity I will assume that we are only going to consider a single light per pixel, and that we are letting the image converge over time.&lt;/p&gt;
&lt;p&gt;The algorithm is fairly simple, and works as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For every pixel, pick a single light using the CDF.&lt;/li&gt;
&lt;li&gt;Calculate the irradiance that the light contributes to the pixel, and divide it by the relative weight of the light.&lt;/li&gt;
&lt;li&gt;Average the light contribution per pixel over many frames to get the correct result.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Step 1: Inverting the CDF&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In an earlier section, I talked about inverting the CDF. This is exactly what we are going to do for step 1 of the algorithm! Let’s take the CDF from a previous example, and invert it. Pretend that A, B, C and D are the lights in our scene. Each light has a radiance, which we are going to use as its weight. This means lights that emit more energy will be stochastically selected by the CDF more often (since they are probably more important than weaker lights).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/04.png.webp&quot; alt=&quot;Inverted CDF for lights A, B, C and D&quot; /&gt;&lt;/p&gt;
&lt;p&gt;First we generate a random float between 0 and 1. Let’s say it landed on 0.75. In the image below, you can see we map this random float to the lights in the CDF and pick whichever one it landed on. In this case, that would be light C. Mapping the random float to the CDF is straightforward: multiply the random float with the sum of all light weights (which is the last element in the CDF) and then do a binary search in the array to find the element that contains the resulting value.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/05.png.webp&quot; alt=&quot;Mapping a random float to a light in the CDF&quot; /&gt;&lt;/p&gt;
&lt;p&gt;In this example, you would be multiplying 0.75 with 5.5, which gives you 4.125. Binary searching would yield you element C because it contains all values between 3.5 and 4.5.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Calculating Irradiance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Cool, we have selected a light! We can now shade our pixel, but there’s still one important step: we must divide the resulting irradiance by the relative weight of our chosen light. Since we did not pick our lights uniformly, some lights will be overrepresented and others will seldom occur. Ignoring this problem would mean we are creating energy out of thin air, which is highly illegal. Fortunately for us, we can calculate the probability of having picked any one light by dividing its weight by the total weight sum of all lights.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/06.png.webp&quot; alt=&quot;Pick probability per light&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We divide the shading result by the pick chance of our light. This will scale the light’s contribution up to compensate for all the lights that we did not pick this frame, proportional to how much the light contributes to the total light power in the scene. Light B would be scaled up less than light A, but B also gets picked more often. In the end this cancels out, and you are left with the same result that you would get from fully random light selection, but you needed less iterations to get to that result.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Algorithm Integration &amp;amp; Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The result from one frame of this algorithm would be noisy, but if you average the results of a few frames it will quickly start to converge. For this scenario where we only have four lights, the algorithm does not make a large difference. But in modern path tracers, having hundreds of thousands of lights is a common scenario. That’s when this algorithm greatly reduces the complexity of shading.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/07.png.webp&quot; alt=&quot;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;cpu-cdf-implementation&quot;&gt;CPU CDF Implementation&lt;/h2&gt;
&lt;p&gt;Now that we have all the theory out of the way, it’s time to actually build our CDF. On the CPU, a naive implementation of an inclusive prefix sum array is very simple:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; light_weights &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; cdf &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-macro syntax-rust&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; sum &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-numeric syntax-rust&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;for&lt;/span&gt; light_weight &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;in&lt;/span&gt; light_weights &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    sum &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; light_weight&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    cdf.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;sum&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Naive CDF / prefix sum implementation on the CPU.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;After executing, the CDF would contain &lt;code&gt;[1.0, 6.0, 8.5, 11.6, 12.6, 14.7]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The real downside to this implementation is that it is single-threaded, and building it takes O(N) time. If we had 16 elements we wanted to calculate the sum of, we would need to perform 15 additions in sequential order since the next addition always depends on the previous result.&lt;/p&gt;
&lt;p&gt;We can then start using the CDF by stochastically selecting elements using some random number generation and a binary search like in the following code snippet:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; Stochastically pick an element in the CDF and return its index.
&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-documentation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;///&lt;/span&gt; The provided `rand` is a uniform random float with a value between 0 and 1.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;binary_search_cdf&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;cdf&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;[&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;], &lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-return-type syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;usize&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; max_value &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; cdf&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;cdf.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; value_to_find &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; rand &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;*&lt;/span&gt; max_value&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; picked_entry &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; left_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; right_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; cdf.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; max_searches &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; cdf.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;log2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;ceil&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;..&lt;/span&gt;max_searches &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; center &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;right_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt; left_index&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; higher &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; cdf&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;center&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; lower &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-float syntax-rust&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-numeric syntax-rust&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; center &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            lower &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; cdf&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;center &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; value_to_find &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt; lower &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            right_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; center &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;if&lt;/span&gt; value_to_find &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt; higher &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            left_index &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; center &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
            picked_entry &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; center&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-rust&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
        &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    picked_entry
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Binary search example on the CPU.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;parallel-prefix-sum&quot;&gt;Parallel prefix sum&lt;/h2&gt;
&lt;p&gt;How do we make the build process faster than O(N)? Introducing: the parallel prefix sum! An algorithm that can calculate the prefix sum of an array of values using many threads at the same time, bringing the complexity down to O(log N). This algorithm requires a large amount of threads to be available at the same time, so it’s a perfect fit for the GPU.&lt;/p&gt;
&lt;p&gt;The algorithm works by building a tree of local summed values in multiple passes. When the tree is completed, each element contains the sum of the elements below it. The top of the tree will contain the sum of all elements.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/08.png.webp&quot; alt=&quot;Binary tree of partial sums&quot; /&gt;&lt;/p&gt;
&lt;p&gt;In our example, we will be building a binary tree from an array of 16 elements, so the algorithm needs 4 (&lt;em&gt;log2(16)&lt;/em&gt;) passes. Each arrow represents a thread adding two values together and storing the result. If you count the arrows, you can see that we still need a total of 15 additions to get the sum of our 16 values. The difference is that we can run the additions largely in parallel! During the first pass, we perform 8 additions, but if we have 8 threads performing them at the same time, the total time taken is the same as doing one addition. Given that we have enough threads, the total runtime will be log2(N) additions where N is the amount of elements we want to sum.&lt;/p&gt;
&lt;p&gt;Now that we have summed our values in tree format, we still need to flatten it into an array where each element contains the prefix sum at the index corresponding to the original weights array.&lt;/p&gt;
&lt;p&gt;This requires one more pass where we launch a thread for each element in our weights array (16 threads in this case). Each thread remembers its element index, and that is where it will eventually write in the CDF array. We traverse the tree top-down, and our goal is to sum all values we find that are only composed of elements that have an index smaller than or equal to the current thread’s element index.&lt;/p&gt;
&lt;p&gt;The logic looks as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is the sum stored at the current tree node composed only of elements with an index smaller than or equal to the current thread’s element index? Then add the sum to our total. If the value already contains the current element index, terminate. We have found our prefix sum. If not, go down the right node of the parent node and repeat the algorithm.&lt;/li&gt;
&lt;li&gt;If the sum stored at the current tree node was composed of elements that lie to the right of our current thread’s element index, then we do not sum this value. Instead go down the node on the left and repeat the algorithm for that node.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/09.png.webp&quot; alt=&quot;Tree traversal for the sixth element&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The image above illustrates this logic for the thread that has been tasked with finding the prefix sum for the sixth element in our array.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It starts at the top of the tree, and realizes that the value there (27) also contains the sum of elements that came after index number six. It goes down the node on the left.&lt;/li&gt;
&lt;li&gt;It checks if the value here (15) lies entirely to the left, but sees that it also contains elements seven and eight, which come after six. It goes down the tree on the left.&lt;/li&gt;
&lt;li&gt;Finally! The sum here (7) is only composed of elements one, two, three and four. It adds 7 to our total sum. This sum does not yet contain our own element (six) so we go down the parents right node.&lt;/li&gt;
&lt;li&gt;The value we find here (8) also sums elements that lie to the right of our current element. We go down the node on the left.&lt;/li&gt;
&lt;li&gt;We find the sum of elements five and six (4). We add it to our total sum, which adds up to 11. Because the current node’s sum includes our current element (six) we can terminate the algorithm and write the prefix sum of index six: 11.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When all threads have finished executing, we are left with our prefix sum array. Knowing all this, it should be somewhat straightforward to implement the parallel prefix sum algorithm in a compute shader. There are however a few fun optimizations we can make!&lt;/p&gt;

&lt;h2 id=&quot;single-pass-tree-builder&quot;&gt;Single Pass Tree Builder&lt;/h2&gt;
&lt;p&gt;First of all, the algorithm requires multiple shader passes, one for each layer of the tree to be more precise. There needs to be an explicit synchronization between these passes because each layer reads from the previous one. Adding these execution barriers into our GPU timeline is slow, especially when we get near the top of the tree where we only need a few threads to do a few operations. Doing multiple compute passes also adds extra overhead.&lt;/p&gt;
&lt;p&gt;Luckily, we can build the entire tree in a single compute pass by using a few HLSL synchronization tricks. This approach is based on &lt;a href=&quot;https://gist.github.com/sebbbi/6cfbec7ab343924dad9b7ee48ef3ba6c&quot;&gt;a code snippet&lt;/a&gt; by Sebastian Aaltonen. I’ve made a greatly simplified version of our CDF tree building shader to showcase how it works:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; All threads in the same group will share the same atomic result.
&lt;/span&gt;groupshared &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; atomic&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_GroupID&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; localThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_GroupIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The tree buffer that we will be reading from and writing to.
&lt;/span&gt;    globallycoherent RWBuffer tree &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; A buffer of atomics initialized to 0. 
&lt;/span&gt;    RWBuffer atomicsBuffer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; treeDepth&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The global thread ID is unique for all threads launched and corresponds to two elements in the tree layer below.
&lt;/span&gt;        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; globalThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; groupId &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; localThreadId&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; readOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; treeOffsets&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; writeOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; treeOffsets&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This thread is only valid if the tree has one or two elements in the layer below that this thread should sum up.
&lt;/span&gt;        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; validThread &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt;..//;
&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;validThread&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Sum the two elements in the previous layer and write them for the current layer.
&lt;/span&gt;            &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; elementReadOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; readOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;globalThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; elementWriteOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; readOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; globalThreadId&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            tree&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;elementWriteOffset&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; tree&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;elementReadOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; tree&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;elementReadOffset &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; One thread per thread group will update the groupshared atomic.
&lt;/span&gt;        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; We halve the groupID so that two thread groups will end up writing to the same atomic.
&lt;/span&gt;        groupId &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;localThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            atomic &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; atomicsBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;interlockedAddUniform&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;atomicsOffsets&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-c++&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; groupId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Sync to ensure that the atomic for this thread group has been updated and will be visible for all threads in the group.
&lt;/span&gt;        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;AllMemoryBarrierWithGroupSync&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Two thread groups wrote to the same atomic, only the last one to write survives for the next tree layer.
&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;atomic &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Sync to make sure that all threads within a group have finished reading the value of `atomic`.
&lt;/span&gt;        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This is needed because it will be modified again in the next iteration.
&lt;/span&gt;        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;GroupMemoryBarrierWithGroupSync&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We start by launching enough thread groups to build the first layer of the tree: one thread for every 2 elements in our CDF array. Each thread will take the two elements from the previous tree layer, and then write a single element away to the next layer in the tree. We then kill off half our thread groups, since we only need to do half the operations in the next layer of our binary tree. Repeat this process until we have only a single thread that writes the top of the tree away.&lt;/p&gt;
&lt;p&gt;The main issue with doing this is that every layer of the tree depends on the values in the previous having been written, so we must ensure that this writing has finished before we start reading.&lt;/p&gt;
&lt;p&gt;The synchronization works because we keep re-using the same thread groups. The thread group that merges the two outputs from the previous layer, is the same thread group that wrote one of those values away in the first place. The other value was written by another thread group, but that group incremented the same atomic value as our current group. Since only the last group to write survives, we know that all data is there when we read it.&lt;/p&gt;
&lt;p&gt;Only the first thread in each thread group updates the atomic in our atomics buffer, and writes the result away to group shared memory. This way all threads within that group can read the value after synchronizing.&lt;/p&gt;
&lt;p&gt;You may have also noticed that I marked the tree buffer as &lt;code&gt;globallycoherent&lt;/code&gt;. This is required because thread groups will be reading values written by other thread groups within the same shader invocation. That means that changes aren’t guaranteed to be written away to global memory from the local caches by the time we read again. The &lt;code&gt;globallycoherent&lt;/code&gt; keyword ensures that any changes we make will be visible to other thread groups.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/10.png.webp&quot; alt=&quot;Thread group lifetimes while building the tree&quot; /&gt;&lt;/p&gt;
&lt;p&gt;To make all this a bit more intuitive, here’s an example of the thread group lifetimes for a tree that is being built for an array containing 8 weights.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We start with four thread groups, each consisting of a single thread for simplicity.&lt;/li&gt;
&lt;li&gt;Group 1 sums weight &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; and stores the result. It increments the atomic, and finds that it is the first to do so. It terminates.&lt;/li&gt;
&lt;li&gt;Group 0 sums weight &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; and stores the result. When incrementing the atomic, it reads back a value of 1, which means the other thread group it’s paired with has already finished executing. It survives because it knows it is the last group to finish.&lt;/li&gt;
&lt;li&gt;In the next iteration group 0 now reads the value it previously wrote, and the value group 1 wrote. From another part of the tree, group 3 is summing its own values and finishes executing first, so it terminates.&lt;/li&gt;
&lt;li&gt;Group 0 is now the last living group. During the last iteration of the tree building, it sums the value it wrote previously and the value written by group 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;optimal-gpu-usage&quot;&gt;Optimal GPU usage&lt;/h2&gt;
&lt;p&gt;We now have a single compute shader invocation that can build our entire CDF tree, but we are not utilizing the GPU in a very efficient way. Having a single thread per thread group is far from optimal, so we should take a closer look at the hardware. It turns out that most modern GPUs schedule thread groups in smaller batches (warps or waves) of around 4 to 64 threads. If we make our thread groups the same size, we will be 100% utilizing the hardware’s thread lanes!&lt;/p&gt;
&lt;p&gt;The next optimization involves wave intrinsics. GPUs execute shader code at exactly the same time for all threads within a wave. Special operations built into HLSL use this to allow you to read variables from other threads in your wave, since they are already loaded into memory anyways. We will be using the powerful &lt;code&gt;WavePrefixSum&lt;/code&gt; intrinsic, which calculates the prefix sum for a variable for all threads in our wave with a lower index than the current thread.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Most GPUs support 32 as wave size. After testing I found this to be the optimal solution on most hardware.
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;WAVE_SIZE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c++&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c++&quot;&gt;WAVE_SIZE_BASE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c++&quot;&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;log2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;WAVE_SIZE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;WAVE_SIZE&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_GroupID&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; localThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; SV_GroupIndex&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; treeDepth&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; globalThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;groupId &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; WAVE_SIZE_BASE&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; localThreadId&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; validThread &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; globalThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; numActiveThreads&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; weight &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;validThread&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            weight &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; tree&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;treeOffsets&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; globalThreadId&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This wave intrinsic calculates the sum of the `weight` variable for all threads with a lower index in this thread group.
&lt;/span&gt;        &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; sum &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;WavePrefixSum&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;weight&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; weight&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; The last thread in the group writes the summed value away.
&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;localThreadId &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; WAVE_SIZE &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            tree&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;treeOffsets&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupId&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; sum&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt;.. All that syncing and atomics stuff we did before ..//
&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I’ve changed the previous code snippet a little bit to show how we would use this. Rather than one thread per group, we now have 32 (the most optimal number differs per GPU). Each thread loads a single element into memory, and the wave intrinsic calculates the sum incredibly fast. The last thread in our group then writes this sum away for the next level of the tree.&lt;/p&gt;
&lt;p&gt;As you may have noticed, our tree is no longer base-2, but base-32. This is great because it means our tree isn’t as deep as it was before. We won’t need as much synchronization and we can process more elements in parallel. This does require some changes to the rest of our algorithm: the offsets for each tree layer will change, as well as the offsets for our atomics. Our thread groups are also no longer writing to an atomic in pairs of two, but in batches of 32. Each batch will be writing to the same atomic, and only the last one to write will survive. This makes sense: one out of every 32 thread groups survives, just like how we have 32 times fewer elements whenever we go up one level in our tree.&lt;/p&gt;
&lt;p&gt;Similarly, the down-sweep pass that flattens the tree into an array will now also need to be updated to consider 32 children per node instead of just two.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bonus optimization:&lt;/strong&gt; since your wave size and tree base are powers of two, you can bit shift indices to the left and right instead of doing multiplication, division or modulo operations. This is a quite common scenario in this algorithm because you will often be calculating the offsets into the tree buffer for a given layer and thread index.&lt;/p&gt;
&lt;h2 id=&quot;layering-multiple-cdfs&quot;&gt;Layering multiple CDFs&lt;/h2&gt;
&lt;p&gt;That is pretty much the entire algorithm out of the way! I did however skip over one important detail of our ray tracing mesh light implementation. Up until this point I’ve been assuming that all lights are neatly laid out in a single array. Of course it can’t be that easy. In our actual implementation, any triangle of a mesh can be emissive, so when we pick a light we need to know the mesh and triangle index.&lt;/p&gt;
&lt;p&gt;The solution here is to add more CDFs. We can build a prefix sum array for the number of emissive triangles in each mesh, and then inverse this CDF by flattening it into an array.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/11.png.webp&quot; alt=&quot;Prefix sum of emissive triangles per mesh&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/12.png.webp&quot; alt=&quot;Flattened array of mesh and triangle indices&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We can now use this flattened array as input for our light CDF tree shader. It will just involve a little bit of extra work for the threads that would normally only read the weights from the bottom level of the tree: they’ll have to load the triangle light using the indices in this buffer, and calculate that weight themselves (and store it for the bottom of the tree).&lt;/p&gt;
&lt;h2 id=&quot;floating-point-precision&quot;&gt;Floating point precision&lt;/h2&gt;
&lt;p&gt;While this algorithm is very fast, it does suffer from a few problems. The most important one is floating point precision. In a scene with a million lights, calculating the prefix sum means adding a lot of floats together. You end up with quite large numbers towards the end, which means lights with tiny weights may not actually change the sum at all anymore. They will thus never be considered. This problem can be mostly mitigated by sorting lights from low to high weight, but such a sorting pass would itself also take up time and may not be worth it.&lt;/p&gt;

&lt;h2 id=&quot;conclusion-timings&quot;&gt;Conclusion &amp;amp; Timings&lt;/h2&gt;
&lt;p&gt;And that is how we stochastically select our lights at Traverse Research! In scenes with &lt;em&gt;~100.000&lt;/em&gt; emissive triangles the whole process of breaking down our meshes and building the CDF for individual emissive triangles takes about &lt;em&gt;0.07&lt;/em&gt; milliseconds on an &lt;em&gt;AMD RX 6800&lt;/em&gt; and &lt;em&gt;0.054&lt;/em&gt; milliseconds on an &lt;em&gt;Nvidia RTX 3080&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I hope this blog post was informative, and inspired you to use these techniques to speed up your own shaders. This was my first time writing a blog post, so feel free to send me some constructive feedback!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/fast-cdf-generation-on-the-gpu-for-light-picking/13.png.webp&quot; alt=&quot;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;CDF light sampling in the Breda framework with ReSTIR for direct and indirect light&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This blog post was first published in 2022.&lt;/em&gt;&lt;/p&gt;
</description></item><item><title>Fixing Rasterization Score for Tiling GPU Architectures</title><link>https://www.evolvebenchmark.com/blog-posts/fixing-rasterization-score-for-tiling-gpu-architectures</link><guid>https://www.evolvebenchmark.com/blog-posts/fixing-rasterization-score-for-tiling-gpu-architectures</guid><description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Evolve is a high-performance benchmark designed to measure the capabilities of modern graphics hardware across desktop and mobile devices.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;On most graphics cards, this measurement is straightforward: for each draw call (a request to render geometry on screen) Evolve is able to time this operation precisely.&lt;/p&gt;
&lt;p&gt;Most mobile chips, however, operate fundamentally differently. To maximize battery life and minimize heat generation, mobile GPUs don&#39;t render the entire screen at once. Instead, they divide the screen into small rectangular sections called &quot;tiles&quot; and process each tile using fast tile memory. This tiling architecture is exceptionally power-efficient, but it fundamentally changes how rendering work is organized.&lt;/p&gt;
&lt;p&gt;When Evolve requested timing information on a tiling GPU, it didn&#39;t receive accurate data. The timestamps only captured a small fraction of the actual work, causing some devices to report artificially inflated scores, up to 10 times higher than their true performance.&lt;/p&gt;
&lt;p&gt;Our solution is to measure the entire rendering phase as one cohesive unit of work, the complete render pass. By measuring the render pass in aggregate all GPU architectures are handled consistently and can report reliably. Unfortunately, as a result, our rasterization scores on mobile will decrease by approximately 10x to reflect actual performance, while desktop scores will see a minor 2-4% reduction due to more comprehensive measurement. Thankfully this correction ensures Evolve provides accurate, comparable performance metrics across all device types.The following sections dive into the technical details of this issue and our solution for those interested in the underlying graphics architecture and implementation specifics.&lt;/p&gt;

&lt;h2 id=&quot;geometry-culling&quot;&gt;Geometry culling&lt;/h2&gt;
&lt;p&gt;At the heart of the rendering pipeline in Evolve, and one of the reasons for its great performance, is an algorithm that’s responsible for removing hidden objects from being submitted to the rasterization pipeline. In Evolve we’ve chosen an algorithm called &lt;em&gt;two-pass occlusion culling&lt;/em&gt; that we have running all the way at the beginning of a particular frame.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;This algorithm runs on the GPU, counts up all objects that ultimately need to be rendered, and will then emit what’s known as &lt;em&gt;indirect&lt;/em&gt; draw calls that end up actually doing the rendering.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;This is an algorithm that we had implemented relatively late into the project and something that deviated from our old way of doing things in which we were culling and recording individual draw calls on the &lt;em&gt;CPU.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;Due to the way this algorithm works and how we structured the internals of the rendering pipeline in Evolve, this had resulted in the following order of operations. (Note that these operations in practice happened relatively far apart from each other).&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;begin&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
	&lt;span class=&quot;syntax-meta syntax-for syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-loop syntax-js&quot;&gt;for&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;bucket&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;indirect&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;draw&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;In the next section, we&#39;ll explore how tiling GPU architectures work, which will reveal why our original approach of recording timestamps around individual indirect draw calls was problematic.&lt;/p&gt;
&lt;h2 id=&quot;understanding-tiling-gpu-architecture&quot;&gt;Understanding Tiling GPU Architecture&lt;/h2&gt;
&lt;p&gt;Tiling architectures exist primarily to save power. Memory access to DRAM is one of the biggest power drains in graphics processing. These accesses eat up bandwidth, draw substantial power, and create heat under load. By keeping data in fast “tile” memory and minimizing trips to system memory, tiling GPUs deliver solid performance while preserving battery life.&lt;/p&gt;
&lt;p&gt;When rendering a frame, a tiling GPU works through all tiles, processing drawing operations for each tile while keeping intermediate results in fast tile memory. This cuts down system memory accesses dramatically compared to rendering the entire screen at once.&lt;br/&gt;&lt;br/&gt;This tiling process happens in two distinct phases: binning and rendering. During binning, the GPU analyzes all geometry in the scene and determines which tiles each piece of geometry touches. In the rendering phase, the GPU processes each tile individually, executing only the drawing commands relevant to that tile while keeping all intermediate data in tile memory. Only when a tile is complete does the GPU (in most cases) write the final result back to system memory.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:702pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-5.png.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that tiling implementations vary by vendor, and the explanation and image above provide only a high-level overview. For detailed information on specific vendor implementations, please refer to the resources listed in the references section at the end of this post.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-measurement-problem&quot;&gt;The measurement problem&lt;/h2&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;Previously, Evolve profiled the time each individual indirect draw command took by querying timestamps before and after every command in the graphics command buffer. After the command buffer completed, we calculated the differences between timestamps to determine each indirect draw&#39;s duration. This approach works correctly on immediate mode GPUs, where draw commands are retired in submission order.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1600pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-6.png.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;&lt;em&gt;Above image shows the render pass VBuffer Raster Opaque Pass (indirect) with correct timing markers for each indirect draw command (vbuffer-indirect_*)  on an immediate mode GPU.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;However, on tiling GPUs, the execution model is fundamentally different. Indirect draws are processed on a per-tile basis, requiring each draw command to be executed across every tile. Accurately measuring a single indirect draw&#39;s duration would require capturing both the up-front binning work, plus the cumulative rendering work across all tiles for just that draw. Graphics APIs don&#39;t expose this level of granularity, nor would it be efficient to query and store timestamps for every tile-draw combination. As a result, on some tiling GPUs, those timestamps are recorded/sampled at the end resulting in infinitely small differences between them, as can be seen in the next screenshot:&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:594pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-8.png.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h2 id=&quot;our-solution&quot;&gt;Our solution&lt;/h2&gt;
&lt;p&gt;We&#39;ve restructured our timing measurements to query timestamps at the render pass boundaries rather than around individual indirect draw commands. At these boundaries, the command buffer is not split and executed multiple times as it is with tiling architectures, providing a consistent measurement point across all GPU architectures.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;begin&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
	&lt;span class=&quot;syntax-meta syntax-for syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-loop syntax-js&quot;&gt;for&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;bucket&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;indirect&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;draw&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Before&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;begin&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
	&lt;span class=&quot;syntax-meta syntax-for syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-loop syntax-js&quot;&gt;for&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;bucket&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;indirect&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;draw&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;pass&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;timestamp&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;After (our solution)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Conceptually, this is what Evolve was already calculating for its rasterization score by summing up all individual indirect draw durations, but now we capture it directly as a single duration over the entire render pass.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1600pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-9.png.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1600pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/image-7.png.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;&lt;em&gt;On above we have the old, incorrect, rasterization timings for tiling GPUs. On below are the correct timings after applying our solution.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;results-on-immediate-mode-gpus&quot;&gt;&lt;em&gt;‍&lt;/em&gt;&lt;br/&gt;Results on Immediate Mode GPUs&lt;/h3&gt;
&lt;p&gt;On immediate mode GPUs, we confirmed that the total render pass duration is only marginally longer than the sum of all individual indirect draws within it. The slight increase in duration (and corresponding decrease in scores) is expected: we now account for render target loading (when not cleared) and storing (writing the final result to memory), operations that were previously unmeasured.&lt;/p&gt;
&lt;h3 id=&quot;results-on-tiling-gpus&quot;&gt;Results on Tiling GPUs&lt;/h3&gt;
&lt;p&gt;On tiling GPUs, our detailed rasterization timing graphs have been transformed. Instead of the flat line indicating near-zero individual indirect draw times, they now follow the same curvature seen on immediate mode GPUs, demonstrating that we&#39;re accurately capturing the workload duration.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.vulkan.org/refpages/latest/refpages/source/VK_QCOM_tile_shading.html#_description&quot;&gt;Vulkan QCOM Tile Shading Extension&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.qualcomm.com/bundle/publicresource/topics/80-78185-2/overview.html&quot;&gt;Qualcomm Adreno GPU Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.arm.com/documentation/102662/0100/Overview&quot;&gt;ARM GPU Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.samsung.com/galaxy-gamedev/resources/articles/gpu-framebuffer.html&quot;&gt;Samsung Galaxy GameDev: GPU Framebuffer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.amd.com/r/en-US/wp557-versal-gen-2-gpu/Tile-Based-Rendering-GPU&quot;&gt;Tile-Based Rendering GPU - WP557&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://interactive.arm.com/story/the-arm-manga-guide-to-the-mali-gpu/&quot;&gt;https://interactive.arm.com/story/the-arm-manga-guide-to-the-mali-gpu/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;‍&lt;/p&gt;
</description></item><item><title>How we wrap external C and C++ libraries in Rust</title><link>https://www.evolvebenchmark.com/blog-posts/how-we-wrap-external-c-and-cpp-libraries-in-rust</link><guid>https://www.evolvebenchmark.com/blog-posts/how-we-wrap-external-c-and-cpp-libraries-in-rust</guid><description>&lt;p&gt;Let&#39;s talk about &lt;strong&gt;&lt;code&gt;build.rs&lt;/code&gt;&lt;/strong&gt; in Rust projects, specifically, how to keep it dead simple, dependency-light, and maintainable, with real-world examples from some solid open source projects. If you’ve ever groaned at the sight of a sprawling CMakeLists.txt or the arcane mysteries of autotools, you’re in the right place. For our project we pretty much have one golden rule, the developer experience should be as close to &lt;code&gt;git clone&lt;/code&gt; + &lt;code&gt;cargo run&lt;/code&gt; as possible when you&#39;re starting on one of our projects. Our developers shouldn&#39;t need to install any additional dependencies (like cmake,  autotools, or worse, or just putting binary libs in arbitrary directories).&lt;/p&gt;
&lt;h2 id=&quot;why-keep-build-rs-simple&quot;&gt;Why keep &lt;strong&gt;&lt;code&gt;build.rs&lt;/code&gt;&lt;/strong&gt; simple?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fewer dependencies:&lt;/strong&gt; Because &lt;code&gt;build.rs&lt;/code&gt; runs early in the build process, having a lot of dependencies here to build before invoking the &lt;code&gt;build.rs&lt;/code&gt; file is mostly just serial overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Predictable builds:&lt;/strong&gt; No surprises from external tools or system packages and more important, no setup docs that go out of date.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Easier onboarding:&lt;/strong&gt; New contributors don’t need to learn or install CMake, autotools, or chase down system packages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reproducibility:&lt;/strong&gt; Vendoring C/C++ code means your build works everywhere, every time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;only-use-cc-and-bindgen&quot;&gt;Only use &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;bindgen&lt;/code&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Seriously, that’s usually all you need. The &lt;a href=&quot;https://crates.io/crates/cc&quot;&gt;cc&lt;/a&gt; crate compiles C/C++ code, and &lt;a href=&quot;https://crates.io/crates/bindgen&quot;&gt;bindgen&lt;/a&gt; generates Rust FFI bindings. No need for &lt;strong&gt;&lt;code&gt;pkg-config&lt;/code&gt;&lt;/strong&gt;, no need for CMake, no need for autotools. Just point &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; at your sources, maybe run &lt;strong&gt;&lt;code&gt;bindgen&lt;/code&gt;&lt;/strong&gt; if you need bindings, and you’re done.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Avoid &lt;code&gt;pkg-config&lt;/code&gt;&lt;/strong&gt; unless you absolutely must. It pulls in system dependencies and can break reproducibility, and most importantly it&#39;s not available in regular windows setups. If you really need it, consider a feature flag so users can opt in and instead prefer building either from source, or including pre-built binaries that work for a wide set of target systems.&lt;/p&gt;
&lt;h2 id=&quot;the-vendored-and-bindgen-features&quot;&gt;The &lt;code&gt;vendored&lt;/code&gt; and &lt;code&gt;bindgen&lt;/code&gt; Features&lt;/h2&gt;
&lt;p&gt;A best practice is to offer a &lt;code&gt;vendored&lt;/code&gt; feature (enabled by default) that builds the C/C++ code you ship in your repo (often as a git submodule). If someone wants to link against a system library instead, they can disable the feature.&lt;/p&gt;
&lt;p&gt;Likewise, a &lt;code&gt;bindgen&lt;/code&gt; feature lets you regenerate bindings if you want, but by default, you should check in the generated Rust bindings. This way, users don’t need to install LLVM or deal with bindgen unless they’re hacking on the FFI. One thing to note: you&#39;ll most likely need differently generated bindgen files for different platforms. Some library&#39;s won&#39;t expose their platform-specific APIs in all platforms (for good reason), and very often you end up mismatching  types (enums are notorious, on some platforms they&#39;re represented as signed ints, and others they&#39;re represented as unsigned ints, etc).&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;example-the-anatomy-of-a-minimal-build-rs&quot;&gt;Example: The anatomy of a minimal &lt;strong&gt;&lt;code&gt;build.rs&lt;/code&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Let’s see how the best projects do it:&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h3 id=&quot;1-add-your-sources&quot;&gt;1. Add your sources&lt;/h3&gt;
&lt;p&gt;Most projects we&#39;ve moved away from &lt;code&gt;cmake&lt;/code&gt; in have one of two helper functions, often called &lt;code&gt;add_sources&lt;/code&gt;, that just lists C/C++ files to compile. Sometimes we&#39;ll walk the directory, other times we&#39;ll just explicitly have a list of files to include. It depends on preference, I prefer a flat file list since it avoids filesystem operations to discover something that&#39;s effectively static at the cost of slight maintainability trade-offs.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;add_sources&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-rust&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;cc&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;Build&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    cfg.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;src/foo.c&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
       .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;src/bar.c&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Or, recursively walk a directory if you have lots of files
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;h3 id=&quot;2-set-up-the-build&quot;&gt;2. Set up the build&lt;/h3&gt;
&lt;p&gt;Here’s the basic pattern you’ll see everywhere; we&#39;ll set up some defines (even platform specific ones), and includes. Occasionally we&#39;ll need to set some compiler-specific flags (check out functions like &lt;a href=&quot;https://docs.rs/cc/latest/cc/struct.Tool.html#method.is_like_msvc&quot;&gt;is_like_msvc&lt;/a&gt; and company for this).&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; build &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;cc&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Build&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;new&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;add_sources&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-rust&quot;&gt;mut&lt;/span&gt; build&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    build.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;include&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    build.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;SOME_DEFINE&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-support syntax-type syntax-rust&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;1&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    build.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;libfoo.a&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;‍&amp;lt;br/&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;3-feature-flags&quot;&gt;3. Feature flags&lt;/h3&gt;
&lt;p&gt;Cargo features let you control whether you build vendored code or use system libraries, and whether you run bindgen. These feature flags can just be passed to cargo like normal (&lt;code&gt;cargo build -F bindgen&lt;/code&gt;) so you can run them as a one-off process.&lt;/p&gt;
&lt;p&gt;For our projects, we&#39;ll typically build with vendored source code, so it&#39;s just a clean and easy batteries-included project to get up and going, at the cost of some compile time.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;feature &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;vendored&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Build and link the vendored C code
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-rust&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-rust&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-rust&quot;&gt;feature &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;vendored&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Link to system library (if you must)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you’re generating bindings, also do it behind a feature flag:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-annotation syntax-rust&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-annotation syntax-rust&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;feature &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;bindgen&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-annotation syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-function syntax-rust&quot;&gt;fn&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-rust&quot;&gt;generate_bindings&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;let&lt;/span&gt; bindings &lt;span class=&quot;syntax-keyword syntax-operator syntax-rust&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;bindgen&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-path syntax-rust&quot;&gt;Builder&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-rust&quot;&gt;::&lt;/span&gt;&lt;/span&gt;default&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;wrapper.h&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        .&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Unable to generate bindings&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
    bindings.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;write_to_file&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;src/bindings.rs&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;syntax-support syntax-function syntax-rust&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string syntax-quoted syntax-double syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;Couldn&amp;#39;t write bindings!&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-rust&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-rust&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But make sure to &lt;strong&gt;check in&lt;/strong&gt; the generated &lt;strong&gt;&lt;code&gt;bindings.rs&lt;/code&gt;&lt;/strong&gt; so users don’t need LLVM or bindgen unless they’re actively working on the FFI. This makes the crate easy to use for all other developers, and it puts the burden on the people that are doing the work, rather than the entire team.&lt;/p&gt;
&lt;h3 id=&quot;4-avoiding-external-build-systems&quot;&gt;4. Avoiding external build systems&lt;/h3&gt;
&lt;p&gt;Don’t call out to CMake, autotools, or anything else. You’re not writing a new build system, you’re just compiling a few files. If you need to mimic a CMake setup, translate the logic into Rust code with &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;build.define&lt;/code&gt;&lt;/strong&gt;, etc. This is way easier to reason about and debug. This is logic in the wrapped project that, thankfully, in our experience doesn&#39;t change often, if at all. Occasionally you&#39;ll see folks add a few files here and there but all in all, nobody likes tinkering with build systems so it&#39;s a pretty reasonable bet that it&#39;s often left alone. Once you set up your &lt;strong&gt;&lt;code&gt;build.rs&lt;/code&gt;&lt;/strong&gt;, you’ll rarely touch it again, unless you update the vendored C/C++ code or tweak a build flag.&lt;/p&gt;
&lt;p&gt;If you’re porting a CMake-based project, just translate the high level logic to Rust: add the  sources, set the defines, and set up the include  directories. It&#39;s often not needed to port over the entire cmake setup as it&#39;s often fille with tons of minutia that might not apply. No need to bring in the whole CMake machinery.&lt;/p&gt;
&lt;h3 id=&quot;5-use-git-submodules&quot;&gt;5. Use git submodules&lt;/h3&gt;
&lt;p&gt;Most of these projects just add the C/C++ library as a git submodule, &lt;code&gt;cargo&lt;/code&gt; will check out these submodules by default so they&#39;re always available for a build and this keeps the code available and versioned,  you can just point&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; at the submodule directory in your &lt;strong&gt;&lt;code&gt;build.rs&lt;/code&gt;&lt;/strong&gt; &lt;code&gt;file&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;

&lt;h2 id=&quot;real-world-examples&quot;&gt;Real-world examples&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/EmbarkStudios/physx-rs/blob/d0d571ee61ea44750bf58df96464f49b466da46e/physx-sys/build.rs&quot;&gt;&lt;strong&gt;physx-sys&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Uses &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; to compile vendored C++ sources, lists files with &lt;strong&gt;&lt;code&gt;add_sources&lt;/code&gt;&lt;/strong&gt;, defines features, and never touches CMake.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/PistonDevelopers/freetype-sys/blob/master/build.rs&quot;&gt;&lt;strong&gt;freetype-sys&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Similar pattern, minimal dependencies, feature flags for vendored/system, and simple source listing.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jean-airoldie/zeromq-src-rs/blob/master/src/lib.rs&quot;&gt;&lt;strong&gt;zeromq-src-rs&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Again, just &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt;, no external build tools, and a clean, readable build script.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/EmbarkStudios/spirv-tools-rs/blob/main/spirv-tools-sys/build.rs&quot;&gt;&lt;strong&gt;spirv-tools-rs&lt;/strong&gt;&lt;/a&gt;: Depends only on cc&lt;a href=&quot;https://github.com/EmbarkStudios/sentry-contrib-rust/blob/main/breakpad-sys/build.rs&quot;&gt;‍&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/EmbarkStudios/sentry-contrib-rust/blob/main/breakpad-sys/build.rs&quot;&gt;&lt;strong&gt;breakpad-sys&lt;/strong&gt;&lt;/a&gt;:Depends only on cc&lt;a href=&quot;https://github.com/LIHPC-Computational-Geometry/metis-rs/blob/410f512740476bac38199a3f3d0ab605cd81fe67/metis-sys/build.rs:&quot;&gt;‍&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/LIHPC-Computational-Geometry/metis-rs/blob/410f512740476bac38199a3f3d0ab605cd81fe67/metis-sys/build.rs:&quot;&gt;&lt;strong&gt;metis-sys&lt;/strong&gt;&lt;/a&gt;: Depends only on bindgen and cc&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;tl-dr&quot;&gt;TL;DR&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Keep &lt;code&gt;build.rs&lt;/code&gt; minimal:&lt;/strong&gt; Only use &lt;strong&gt;&lt;code&gt;cc&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;bindgen&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Feature flags:&lt;/strong&gt; For vendored/system and bindgen/no-bindgen.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No external build systems:&lt;/strong&gt; No CMake, no autotools, no pkg-config if you can help it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vendor C/C++ code via git submodules.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check in generated bindings.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Just compile a few files and link them.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you want to keep your Rust project as simple as &lt;strong&gt;&lt;code&gt;cargo build&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;cargo run&lt;/code&gt;&lt;/strong&gt;, this is the way. Life’s too short to debug CMake errors on a Friday afternoon. Keep it simple, keep it Rusty!&lt;/p&gt;
&lt;h2 id=&quot;final-pro-tip&quot;&gt;Final pro-tip&lt;/h2&gt;
&lt;p&gt;Do enable the &lt;code&gt;cc&lt;/code&gt; feature &lt;code&gt;parallel&lt;/code&gt; , this feature used to drag in &lt;code&gt;rayon&lt;/code&gt; which would serialize the compilation all the way at the beginning of the build process, however, over the years they&#39;ve switched it out to &lt;code&gt;jobserver&lt;/code&gt; which is a much lighter dependency! Also note that due to the way cargo build features work, if one of the crates enables this, most likely all of them will have it enabled anyway!&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
</description></item><item><title>Inline vs Pipeline Ray Tracing</title><link>https://www.evolvebenchmark.com/blog-posts/inline-vs-pipeline-ray-tracing</link><guid>https://www.evolvebenchmark.com/blog-posts/inline-vs-pipeline-ray-tracing</guid><description>&lt;h2 id=&quot;what-is-the-difference-between-pipeline-and-inline-ray-tracing&quot;&gt;What is the difference between Pipeline and Inline Ray Tracing?&lt;/h2&gt;
&lt;p&gt;One of the most frequent questions we get is: What is the difference between the pipeline and inline ray tracing modes in Evolve? In short, they are two different approaches to solving the same problem, each with trade-offs. To really answer this question, we will dive deep in this blog post on how they both work, how they differ, and how they perform on different GPUs.&lt;/p&gt;
&lt;h2 id=&quot;in-a-nutshell-what-is-the-difference-between-inline-and-pipeline-ray-tracing&quot;&gt;In a nutshell: What is the difference between inline and pipeline Ray Tracing?&lt;/h2&gt;
&lt;p&gt;To start, let’s do a quick recap of what ray tracing is. Ray tracing allows us to choose a position in a 3D scene, choose a direction, and travel along a line in that direction until we hit something in that scene. We then find out what we hit, and how far the thing was that we hit. This lets us simulate how rays of light would traverse through a 3D scene.&lt;/p&gt;
&lt;p&gt;For example, we can simulate a light photon by starting at a light source, traveling in the direction of that light into the scene, hitting an object, then tracing a second ray that travels to the camera, and then calculating how much energy is reflected from the light to the surface, to the camera.&lt;/p&gt;
&lt;p&gt;All of these lighting computations are performed by small GPU programs called shaders. Rendering engines use shaders to determine object positions relative to the camera, carry out per-pixel calculations, or even perform more general-purpose computations. Before ray tracing, there were already five types of shaders for rasterization (three of those being optional), as well as one for general computations. Inline ray tracing lets programmers add ray tracing to these existing shaders. This has the benefit of being relatively easy to integrate into existing algorithms, but it limits the GPU in how it schedules the ray tracing operations.&lt;/p&gt;
&lt;p&gt;Pipeline ray tracing takes a different approach by introducing five new shader types just for ray tracing. This model does allow for hardware designs that can take much more freedom in scheduling all the ray tracing operations efficiently, but it does come with the downside that it is not as easy to integrate into existing systems and can introduce overhead when the ray tracing operation needs to be performed from a different shader than the one where the result was originally needed.&lt;/p&gt;
&lt;h2 id=&quot;benchmarking-inline-vs-pipeline-ray-tracing&quot;&gt;Benchmarking Inline vs Pipeline Ray Tracing&lt;/h2&gt;
&lt;p&gt;When designing and building the Evolve benchmark, we wanted to be able to measure the performance difference between inline and pipeline ray tracing. Evolve implements all ray tracing algorithms using both the inline and pipeline-based ray tracing techniques, meaning that we can get as close to a direct comparison as possible with various workloads like shadows, reflection, refractions, and global illumination.&lt;/p&gt;
&lt;p&gt;We ran Evolve on three GPUs to compare their inline and pipeline ray tracing score. The goal here is not to say which GPU is the better value, but rather to take a look at how different techniques perform per GPU.&lt;/p&gt;
&lt;p&gt;We used these graphics cards:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sapphire Nitro+ AMD Radeon RX 9070 XT 16GB with driver 25.9.1&lt;/li&gt;
&lt;li&gt;MSI NVIDIA GeForce RTX 5070 Ti 16GB Vanguard SOC with driver 581.29&lt;/li&gt;
&lt;li&gt;Sparkle Intel Arc B580 TITAN AC 12GB with driver 32.0.101.7029&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of our test setup consisted of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CPU: AMD Ryzen 7 9800X3D&lt;/li&gt;
&lt;li&gt;Motherboard: ASUS ROG Strix B650E-F GAMING WIFI&lt;/li&gt;
&lt;li&gt;Memory: 32 GiB DDR5 Kingston Fury Beast @ 6000MT/s&lt;/li&gt;
&lt;li&gt;Power Supply: Corsair RM1000x&lt;/li&gt;
&lt;li&gt;CPU Cooler: Arctic Liquid Freezer III Pro 360&lt;/li&gt;
&lt;li&gt;Storage: WD Black SN770 1TB&lt;/li&gt;
&lt;li&gt;OS: Windows 11 24H2&lt;/li&gt;
&lt;li&gt;Benchmark: Evolve v1.0.10&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:952pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/inline-vs-pipeline-ray-tracing-score.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;Before we ran the test, we were expecting to get fairly similar scores for both inline and pipeline ray tracing, which was true for the NVIDIA GeForce RTX 5070 Ti and the Intel Arc B580. However, on the AMD Radeon RX 9070 XT, we see that pipeline ray tracing performs much better than inline! This result is surprising to us for two reasons.&lt;/p&gt;
&lt;p&gt;The first reason has to do with the fact that Evolve uses a single material shader architecture. Game engines can choose to have a shader per material or one shader with a lot of parameters. Pipeline ray tracing is great for this many-material systems as more closest hit shaders can be added as needed, while inline ray tracing would require processing the results in separate passes. However, with Evolve’s single material system we expected to see very similar performance between inline and pipeline ray tracing.&lt;/p&gt;
&lt;p&gt;The second reason is that, from what we can gather, AMD compiles all the pipeline ray tracing shaders into one big shader. This would suggest to us that AMD could use different strategies for compiling the shader code between inline and pipeline ray tracing, but we can’t be certain of that.&lt;/p&gt;
&lt;h2 id=&quot;a-deep-dive&quot;&gt;A Deep Dive&lt;/h2&gt;
&lt;p&gt;To give a more in-depth look into inline and pipeline ray tracing we will look at how a ray is traced in the first place, and what type of cases need to be handled by the game engine. At its core the GPU needs a ray and a scene acceleration structure to trace the ray in. It will then find the closest primitive in the scene that intersects with the provided ray.&lt;/p&gt;
&lt;p&gt;This will do great for most 3D geometry, but not everything. For example, plants like grass are usually not modelled blade by blade, but instead as a bigger rectangle with a texture that uses transparency to only draw the blades of grass themselves. This means that in order for it to count as a hit, the ray doesn’t need to just intersect with one of the triangles; it also needs to check if that part of the texture is transparent or not. Both inline and pipeline ray tracing techniques support this, but in slightly different ways.&lt;/p&gt;
&lt;p&gt;Another case that needs to be handled is non-triangle geometry since it’s not possible to handle all types of geometry in hardware. Although the vast majority of geometry used in games are triangles, not everything translates that well into triangles. For example, hair or volumetric objects don’t translate well into triangles at all. For this reason a procedural primitive was added. By adding bounding boxes surrounding the procedural primitives to the scene acceleration structure, the GPU will know when it could have potentially hit a piece of procedural geometry. When a ray hits one of these bounding boxes it will need to run some code to determine if it hit the shape inside or not. Just like with handling partially transparent triangles, both inline and pipeline ray tracing support this case but in different ways.&lt;/p&gt;
&lt;p&gt;Let’s take a look at how inline ray tracing can be added into existing shaders and how these special cases can be handled.&lt;/p&gt;
&lt;h2 id=&quot;understanding-inline-ray-tracing&quot;&gt;Understanding Inline Ray Tracing&lt;/h2&gt;
&lt;p&gt;In inline ray tracing the main design is to able to tell the GPU to trace a ray, get a result back. Based on the result the shader might get told that a special case needs to be handled or if the ray is done tracing. Let’s first look at the simplest case, where we assume that there is no partially transparent geometry and no procedural geometry.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;Note that the lines starting with // are comments explaining what lines do&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Set up the ray query for ray tracing; nothing actually happens here yet.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;RayQuery&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;TraceInline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;accelerationStructure&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;instanceMask&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;ray&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Perform the ray tracing
&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;Proceed&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Run different code based on the hit result.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;CommitedStatus&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_TRIANGLE_HIT&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray hit a triangle, add code for shading here
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;else if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_NOTHING&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray missed the scene, add code for shading the sky here
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Proceed function returns a value that determines if the ray has finished tracing or if it found a candidate, but needs help deciding if the hit counts or not. If the proceed function tells us that it needs help we can check if the candidate is a non opaque triangle and handle that case accordingly.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Set up the ray query for ray tracing; nothing actually happens here yet.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;RayQuery&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;TraceInline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;accelerationStructure&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;instanceMask&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;ray&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Perform the ray tracing until the GPU tells us we are done
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-while syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-loop syntax-js&quot;&gt;while&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;Proceed&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;CandidateType&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;CANDIDATE_NON_OPAQUE_TRIANGLE&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Use code to determine if hit should count or not
&lt;/span&gt;
&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Run different code based on the hit result.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;CommitedStatus&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_TRIANGLE_HIT&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray hit a triangle, add code for shading here
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;else if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_NOTHING&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray missed the scene, add code for shading the sky here
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Handling procedural geometry can be handled just like non opaque geometry by checking for the candidate type and running some code to determine if it is a hit or not. After that, when the ray has completed tracing, there is committed status just for procedural geometry to use different shading code, but a game engine may also decide to use the same shading code as for triangles.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Set up the ray query for ray tracing; nothing actually happens here yet.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;RayQuery&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;TraceInline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;accelerationStructure&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;instanceMask&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;ray&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Perform the ray tracing until the GPU tells us we are done
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-while syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-loop syntax-js&quot;&gt;while&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;Proceed&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;CandidateType&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;CANDIDATE_NON_OPAQUE_TRIANGLE&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Use code to determine if hit should count or not
&lt;/span&gt;    &lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;else if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;CANDIDATE_PROCEDRUAL_PRIMITIVE&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Use code to determine if ray hits procedural primitive
&lt;/span&gt;

&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Run different code based on the result.
&lt;/span&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;CommitedStatus&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_TRIANGLE_HIT&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray hit a triangle, perform shading
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;else if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_NOTHING&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray missed the scene, calculate sky color
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-conditional syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-conditional syntax-js&quot;&gt;else if&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-js&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;COMMITTED_PROCEDURAL_HIT&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-js&quot;&gt;//&lt;/span&gt; Ray hit a procedural primitive, perform shading
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inline Ray Tracing makes it fairly simple to introduce new ray tracing code with just a handful of lines of code. The only downside is that because the logic needs to keep coming back to the same shader for special cases, the GPU doesn’t have as much freedom in deciding when to execute what code. To see how we can give the GPU more flexibility let’s take a look at pipeline ray tracing.&lt;/p&gt;

&lt;h2 id=&quot;exploring-pipeline-ray-tracing&quot;&gt;Exploring Pipeline Ray Tracing&lt;/h2&gt;
&lt;p&gt;Pipeline Ray Tracing takes a different approach by introducing five new types of shaders specifically designed for ray tracing. The new shader types are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ray Generation Shader&lt;/li&gt;
&lt;li&gt;Miss Shader&lt;/li&gt;
&lt;li&gt;Closest Hit Shader&lt;/li&gt;
&lt;li&gt;Any-Hit Shader&lt;/li&gt;
&lt;li&gt;Intersection Shader&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;We start with a Ray Generation Shader, where initial rays are generated and a TraceRay() function is called. We give TraceRay a scene acelleration structure to traverse and a ray. Additionally, a ray payload is also provided that can be read and written to from the other shaders while the ray is going through the pipeline. The GPU will then use the acceleration structure to find primitives that the ray intersects with and launch the other shader types as needed.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1323pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/pipeline-ray-tracing-diagram.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A diagram showing the control flow of the shades in the pipeline&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Let’s say we have a ray that doesn’t hit any object in the scene. The GPU will launch a miss shader for that ray. In that miss shader, where for example the sky color can be computed.&lt;/p&gt;
&lt;p&gt;When a ray does intersect with the scene and the GPU has determined which primitive is the closest one, it will invoke the closest hit shader. In the closest hit shader, we can calculate how the surface of that object should be shaded.&lt;/p&gt;
&lt;p&gt;Just like with inline ray tracing, when a ray hits non-opaque geometry, it will need to know whether to count the hit or not. The GPU will dispatch the any hit shader where some code can determine if the ray hit a transparent section or not, and decide to ignore or keep the hit.&lt;/p&gt;
&lt;p&gt;Pipeline ray tracing also supports handling procedural geometry. This is done by launching an intersection shader. In the intersection shader, custom shader code can determine if the ray hits the geometry or not. After that, the GPU continues tracing the ray and will end with calling the closest hit or miss shader.&lt;/p&gt;
&lt;p&gt;One important aspect we haven’t touched so far is that in a ray tracing pipeline, there may be many closest hit, miss, any-hit, and intersection shaders. For example, a different closest hit shader may be needed depending on whether a material is made of glass or metal. In order for the GPU to know which shader needs to be launched, a Shader Binding Table (SBT) is used. Using an instance index and a geometry index, the GPU can find an entry in this table and launch the corresponding shader.&lt;/p&gt;
&lt;p&gt;The big advantage that pipeline ray tracing brings is that if the GPU finds hits, or needs to run intersection shaders, it doesn’t need to execute that as it finds them. It can gather similar shaders that need to be run, and then run a bunch of them at the same which, which GPUs are generally better at. However, current hardware seems to do this only in very limited amounts. That does not mean that future hardware won’t get better at this in the future.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Inline and Pipeline Ray Tracing both offer advantages and disadvantages between each other. One is not necessarily better than the other, but they are different ways of doing the same thing. In our Evolve Benchmark, we are interested in finding out how different GPUs perform with these different approaches and how this changes throughout the years. That’s why we have implemented all our ray tracing-based graphics algorithms with both the Inline and the Pipeline Ray Tracing methods. From some benchmarks we ran with Evolve we don’t see any major performance differences between inline and pipeline on Nvidia and Intel, while on AMD Pipeline Ray Tracing is much faster. Only time will tell how this will change as better hardware and better drivers are released.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.intel.com/content/www/us/en/developer/articles/guide/real-time-ray-tracing-in-games.html&quot;&gt;https://www.intel.com/content/www/us/en/developer/articles/guide/real-time-ray-tracing-in-games.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gitlab.freedesktop.org/mesa/mesa/-/blob/a905ff3d544b94f5996e2eabb91a0137efd2a1cf/src/amd/vulkan/nir/radv_nir_rt_common.c&quot;&gt;https://gitlab.freedesktop.org/mesa/mesa/-/blob/a905ff3d544b94f5996e2eabb91a0137efd2a1cf/src/amd/vulkan/nir/radv_nir_rt_common.c&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna4-instruction-set-architecture.pdf&quot;&gt;https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna4-instruction-set-architecture.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://images.nvidia.com/aem-dam/Solutions/geforce/ada/nvidia-ada-gpu-architecture.pdf&quot;&gt;https://images.nvidia.com/aem-dam/Solutions/geforce/ada/nvidia-ada-gpu-architecture.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3/vkspec.html&quot;&gt;https://vulkan.lunarg.com/doc/view/1.3.283.0/mac/1.3/vkspec.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html&quot;&gt;https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
</description></item><item><title>Intro to rendering of animated volumetric models in Breda</title><link>https://www.evolvebenchmark.com/blog-posts/intro-to-rendering-of-animated-volumetric-models-in-breda</link><guid>https://www.evolvebenchmark.com/blog-posts/intro-to-rendering-of-animated-volumetric-models-in-breda</guid><description>&lt;p&gt;In this post, we’ll go over the basic building blocks of our real-time volumetric rendering process in the Breda graphics framework (developed at &lt;a href=&quot;https://traverse.nl/&quot;&gt;Traverse Research&lt;/a&gt;). This post is aimed at people experienced with general graphics algorithms like ray marching, writing shaders and some experience with bit manipulations. The following design goals were made very early in the development process and have guided the implementation so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Loading of OpenVDB files&lt;/li&gt;
&lt;li&gt;Index space of 4096³&lt;/li&gt;
&lt;li&gt;Animation playback&lt;/li&gt;
&lt;li&gt;Voxels represent floating point data&lt;/li&gt;
&lt;li&gt;Small memory footprint to have enough space left over for other assets&lt;/li&gt;
&lt;li&gt;Needs to be ray traced in real time on an RTX 3070&lt;/li&gt;
&lt;li&gt;Multiscattering and emission support (not in this blog post)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/intro-to-rendering-of-animated-volumetric-models-in-breda/01.png.webp&quot; alt=&quot;Disney cloud rendered using the Breda framework&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&quot;https://disneyanimation.com/resources/clouds/&quot;&gt;Disney cloud&lt;/a&gt; rendered using the Breda framework&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;our-data-structure&quot;&gt;Our data structure&lt;/h2&gt;
&lt;p&gt;Our data structure is a trimmed down version of the &lt;a href=&quot;https://www.openvdb.org/&quot;&gt;VDB&lt;/a&gt; data structure. We load VDB files using our custom parser called &lt;a href=&quot;https://github.com/Traverse-Research/vdb-rs&quot;&gt;vdb-rs&lt;/a&gt;, and then load our structure into a format that includes only the features that we require. As with most VDB implementations we use the 5–4–3 structure which means that our top level node has an index space of 32³ (&lt;code&gt;1&amp;lt;&amp;lt;5&lt;/code&gt;), our first internal node has an index space of 16³ (&lt;code&gt;1&amp;lt;&amp;lt;4&lt;/code&gt;) and our last internal node has an index space of 8³ (&lt;code&gt;1&amp;lt;&amp;lt;3&lt;/code&gt;). These combined result in an index space of 4096³ (&lt;code&gt;1&amp;lt;&amp;lt;5+4+3&lt;/code&gt;). From now on, the different nodes will be referred to by their size, so Node5 is the top level node and Node3 the bottom level node. Below is a schematic of the structure:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/intro-to-rendering-of-animated-volumetric-models-in-breda/02.png.webp&quot; alt=&quot;Schematic of the 5-4-3 node structure&quot; /&gt;&lt;/p&gt;
&lt;p&gt;These nodes are split up into 2 parts: a header, and a bitmask. Below is a description of one of these nodes, here the size of the bitmask is dependent on the level at which this node is located in the structure:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-struct syntax-rust&quot;&gt;struct&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-rust&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;child_pointer&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;,   &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Points to the start of N_CHILDREN child nodes
&lt;/span&gt;  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;tile_value&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;,      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; When the entire region is homogeneous
&lt;/span&gt;  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;min_value&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;,       &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Minimum value of all nodes below it
&lt;/span&gt;  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;max_value&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;,       &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Maximum value of all nodes below it
&lt;/span&gt;  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;mean_value&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;f32&lt;/span&gt;,      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Mean value of all nodes below it
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; Mask where every bit determines if one child node either 
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-rust&quot;&gt;//&lt;/span&gt; contains data or not
&lt;/span&gt;  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;child_mask&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; [&lt;span class=&quot;syntax-storage syntax-type syntax-rust&quot;&gt;u32&lt;/span&gt;; N_CHILDREN / 32],
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All of these nodes have to be stored somewhere in a GPU friendly way, and thus we create a structure which we can upload to the GPU. This structure contains the following information:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-struct syntax-rust&quot;&gt;struct&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-struct syntax-rust&quot;&gt;Volume&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-struct syntax-rust&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;constants&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;VolumeConstants&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;nodes5&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Node5&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;nodes4&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Node4&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;nodes3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-generic syntax-rust&quot;&gt;Vec&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust&quot;&gt;&amp;lt;&lt;/span&gt;Node3&lt;span class=&quot;syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;,
  &lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-rust&quot;&gt;voxel_data&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-rust&quot;&gt;:&lt;/span&gt; VoxelData,
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-rust&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-rust&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As can be seen, we have vectors for the different node types. Additionally, we have a vector of constants, this contains information about the translation, scale and aabb of our volume. The reason why this is a vector is because these constants can be unique per animation frame. The last piece of the puzzle is our voxel data, which is stored in a large buffer containing the actual floating point data that the Node3 point to. This data is stored in bricks of 8³ voxels. Since this is by far the part of our data structure that consumes the most memory, our goal was to optimize it as much as possible. This is achieved by first changing the layout from a flat array (where each brick is stored as 8³ consecutive values) to a 3d texture layout, and then by storing the values in such a way that each 4 values in the z direction (of the brick) are stored as the RGBA components of our 3d texture. This allows us to run BC7 block compression and end up with 2 bits per voxel (floating point value). Below is a slice of the resulting texture:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/intro-to-rendering-of-animated-volumetric-models-in-breda/03.png.webp&quot; alt=&quot;Slice of the BC7 compressed voxel data texture&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Here each chunk of 8² pixels is half of an 8³ chunk, two of these slices make up the entire chunk. As can be seen, there are visible edges, corners and swirls in our data, even at this incredibly small scale.&lt;/p&gt;
&lt;h2 id=&quot;animations&quot;&gt;Animations&lt;/h2&gt;
&lt;p&gt;As said earlier, we support animated volumes. These animations consist of sequences of VDB files which can be exported by programs like EmberGen or Houdini, or downloaded directly from &lt;a href=&quot;https://jangafx.com/software/embergen/download/free-vdb-animations/&quot;&gt;JangaFX’s website&lt;/a&gt; (like we did in our examples below). You might have noticed that our Node5’s are also stored in a vector, but each of those nodes already has our desired index space of 4096³. So, instead, each of our animation frames has a unique Node5. This way, when switching to the next animation frame, we only have to change the starting point of our structure traversal.&lt;/p&gt;

&lt;h2 id=&quot;structure-sampling&quot;&gt;Structure sampling&lt;/h2&gt;
&lt;p&gt;Raymarching through volumetric space requires sampling the data structure a lot of times. This will likely be the hot-loop of our shader, so this has to be fast.&lt;/p&gt;
&lt;p&gt;Sampling our structure requires checking if a child node is valid, and then follow the pointer down the tree. If the child node is &lt;em&gt;not&lt;/em&gt; valid, we return the tile value, which indicates the value of all nodes beneath it. Once we are sure that the voxel exists, by checking the bit in our Node3, we sample our voxel data texture. Below is our code for doing that, note that all multiply, division and modulo operations are done using powers of 2, thus these are compiled to bit shifts and bitwise and operations.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;getVoxel&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Assume we are in the aabb of the volume
&lt;/span&gt;  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; indexNode5 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;computeIndexNode5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;position&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getBitNode5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;indexNode5&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; animationFrame&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;tileValueNode5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;animationFrame&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; indexNode4 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;computeIndexNode4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;position&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; node5ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getChildPointerNode5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;animationFrame&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; node4Index &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; node5ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode5Children&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; indexNode5&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getBitNode4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;indexNode4&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; node4Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;tileValueNode4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;node4Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; indexNode3 &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;computeIndexNode3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;position&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; node4ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getChildPointerNode4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;node4Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; node3Index &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; node4ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode4Children&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; indexNode4&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getBitNode3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;indexNode3&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; node3Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;tileValueNode3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;node3Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; node3ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;getChildPointerNode3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;node3Index&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Our texture supports a maximum of kMaxBricksInDim^3 bricks, 
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; so wrap the indices to that region
&lt;/span&gt;  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; node3ChildPointer to brick texture coordinates
&lt;/span&gt;  uint3 brickIndex&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  brickIndex&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;node3ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kMaxBricksInDim&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  brickIndex&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;node3ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kMaxBricksInDim&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kMaxBricksInDim&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  brickIndex&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; node3ChildPointer &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kMaxBricksInDim&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kMaxBricksInDim&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  brickIndex &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;uint3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; multiplied by brick size, 
&lt;/span&gt;                                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; each z texel stores 4 values
&lt;/span&gt;
  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Offset inside brick
&lt;/span&gt;  uint3 texel&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  texel&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; position&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  texel&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; position&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  texel&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;position&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  uint3 toSample &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; brickIndex &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;|&lt;/span&gt; texel&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Position sample correctly in texture
&lt;/span&gt;  float4 chunkData &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; voxelData&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;load3D&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;float4&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;toSample&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Grab correct z slice out of float4
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; chunkData&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;position&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One function that needs further explaining is &lt;code&gt;computeIndexNode&lt;/code&gt;. This is exactly the same as the &lt;code&gt;internalOffset&lt;/code&gt; calculation in the OpenVDB paper. As input we get a position, then for every dimension we select only the bits that are relevant to the layer we are calculating the offset for. Then we place the selected bits into a single unsigned integer which is the the local index. Below is a code example which does this:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;computeIndexNode5&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;uint3 &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
  uint3 selected &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;position &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode5SLog&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode3Log&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode4Log&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;&amp;lt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode5Log&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode5Log&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;&amp;lt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-other syntax-variable syntax-mac-classic syntax-c&quot;&gt;kNode5Log&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;&amp;lt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;|&lt;/span&gt; selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;|&lt;/span&gt; selected&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a number of constants: &lt;code&gt;kNode5SLog&lt;/code&gt; describes the log2 span of our Node5 (3+4+5=12) and the different &lt;code&gt;kNodeXLog&lt;/code&gt; are simply the X values (so &lt;code&gt;kNode5Log&lt;/code&gt; = 5).&lt;/p&gt;
&lt;h2 id=&quot;simple-rendering&quot;&gt;Simple rendering&lt;/h2&gt;
&lt;p&gt;To get some simple rendering going with our data structure, we can employ techniques commonly used in the game industry, as seen (for example) in 2.5D clouds rendering. For an in-depth overview of those methods, the challenges faced, and the solutions employed, we refer the reader to the extensive work available from &lt;a href=&quot;https://sebh.github.io/publications/&quot;&gt;Sébastien Hillaire&lt;/a&gt; as well as the various &lt;a href=&quot;https://www.guerrilla-games.com/read/nubis-realtime-volumetric-cloudscapes-in-a-nutshell&quot;&gt;Nubis-related&lt;/a&gt; talks from Guerrilla Games.&lt;/p&gt;
&lt;p&gt;Participating media is composed of particles which either scatter or absorb photons. Due to the extremely high amount of particles in a volume, this is all modelled through a statistical process in which the different kinds of particles are described by absorption (&lt;strong&gt;μₐ&lt;/strong&gt;) and scattering (&lt;strong&gt;μₛ&lt;/strong&gt;) coefficients.&lt;/p&gt;
&lt;p&gt;A beam of light travelling through a differential volume element can undergo the following processes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Absorption&lt;/li&gt;
&lt;li&gt;Out-scattering&lt;/li&gt;
&lt;li&gt;In-scattering&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The change of radiance travelling towards the ray direction must also account for emission, but we ignore it for this article to keep things contained.&lt;/p&gt;
&lt;p&gt;We also focus on a single in-scattering event for the time being: the amount of light scattered back to the view as a result of the interaction between a light (in our case, the sun) and the media.&lt;/p&gt;
&lt;p&gt;Absorption and out-scattering “remove” photons from the light ray travelling towards our eye, either because photons are absorbed or because they bounce off of the volume particles and away from the view. This process is a function of both the absorption and scattering coefficients, which (summed up) make up the &lt;strong&gt;extinction coefficient&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This can be represented with a single differential process, integrated along the ray direction, which gives us the &lt;strong&gt;transmittance&lt;/strong&gt; along the ray as a function of the extinction coefficient. This is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Beer%E2%80%93Lambert_law&quot;&gt;Beer Lambert law&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In-scattering is a function of both the scattering coefficient and the phase function. The phase function describes the angular distribution of scattered rays for a volume, and is conceptually similar to a BRDF for surfaces.&lt;/p&gt;
&lt;p&gt;We now have all the tools to understand the volume rendering equation&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/intro-to-rendering-of-animated-volumetric-models-in-breda/04.png.webp&quot; alt=&quot;The volume rendering equation&quot; /&gt;&lt;/p&gt;
&lt;p&gt;which tells us that the incoming radiance from point &lt;strong&gt;x&lt;/strong&gt; towards the camera (&lt;strong&gt;ω&lt;/strong&gt;) is the result of an integration process (with upper bound the surface point &lt;strong&gt;z&lt;/strong&gt;) in which, at each step, the in-scattered radiance is modulated by the transmittance accumulated up to that step, and by the scattering coefficient at &lt;strong&gt;y&lt;/strong&gt;. The exitant radiance at the surface &lt;strong&gt;z&lt;/strong&gt; also contributes to the final output and is modulated by the total transmittance accumulated along the ray (the volume “translucency”).&lt;/p&gt;
&lt;p&gt;We can approximate this integral as a Riemann sum, using ray marching.&lt;/p&gt;
&lt;p&gt;Notice that transmittance is multiplicative along a ray&lt;/p&gt;
&lt;p&gt;T(a→c) = T(a→b) · T(b→c)&lt;/p&gt;
&lt;p&gt;which means that we can simply accumulate transmittance that way at each step of our integration.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/intro-to-rendering-of-animated-volumetric-models-in-breda/05.png.webp&quot; alt=&quot;Accumulating transmittance along a ray&quot; /&gt;&lt;/p&gt;
&lt;p&gt;With all that said, a simple (and naive) sampling routine would look like this:&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; each pixel &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;

  TraceContext ctx &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;computePrimaryRay&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; t0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; t1&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;computeVolumeIntersectionBounds&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;out t0&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; out t1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Variable needed for our final output
&lt;/span&gt;  float3 transmittance &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  float3 scatteredLight &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Results of our surface passes
&lt;/span&gt;  float3 reflectedRadiance &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;loadReflectedRadiance&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Better approach: start offset on view-aligned planes
&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; each step with `stepSize` starting at t0 &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Compute scattering location
&lt;/span&gt;    float3 pos &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; ctx&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;origin&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; ctx&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;t0 &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; steppingDist&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Sample medium density at location
&lt;/span&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; density &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; volume&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;sampleDensity&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;density &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Calculate absorption, scattering and extinction coefficients
&lt;/span&gt;      VolumeCoefficients coeff &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; volume&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;getCoefficients&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;density&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Compute direct lighting
&lt;/span&gt;      float3 directLighting&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Secondary volumetric trace towards the light
&lt;/span&gt;        float3 volumetricShadow &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;traceShadowRay&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;pos&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; dirToSun&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Compute phase function value
&lt;/span&gt;        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;float&lt;/span&gt; phaseFunction &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; volume&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;phaseFunctionEval&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;dirToSun&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;ctx&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;dir&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        directLighting &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; SUN_INTENSITY &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; phaseFunction &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; volumetricShadow&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

      float3 incomingLight &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; coeff&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;scattering&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; directLighting&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

      float3 stepTransmittance &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-support syntax-function syntax-C99 syntax-c&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;coeff&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;extinction&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; stepSize&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Improved integration for lower sample counts as presented by Sébastien Hillaire
&lt;/span&gt;      float3 integrationStep &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;incomingLight &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; incomingLight &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; stepTransmittance&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;/&lt;/span&gt; coeff&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;extinction&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
      scatteredLight &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; integrationStep &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; transmittance&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Accumulate transmittance
&lt;/span&gt;      transmittance &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;*=&lt;/span&gt; stepTransmittance&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

  float3 result &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; reflectedRadiance &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; transmittance &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; scatteredLight&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;writeOutput&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;result&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This blog post went over the foundation of our real time volume renderer. Below is a demo of the renderer of a standalone volume. We can easily play back animations or manually change to the desired animation frame.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=pUkvbD0j8lM&quot;&gt;Watch the standalone volume demo on YouTube&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Below, we see a render from our Breda renderer. Here we placed a volumetric animation inside the Bistro scene.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=uZ53LaWfU9c&quot;&gt;Watch the volumetric animation in the Bistro scene on YouTube&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This blog post was written by &lt;a href=&quot;https://twitter.com/EmilioLaiso&quot;&gt;Emilio Laiso&lt;/a&gt; and &lt;a href=&quot;https://x.com/J__Winther&quot;&gt;Jasper de Winther&lt;/a&gt; at Traverse Research. If you still have any questions, feel free to reach out on Twitter or directly to Traverse &lt;a href=&quot;https://traverse.nl/inquiries&quot;&gt;through our website&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This blog post was first published in 2023.&lt;/em&gt;&lt;/p&gt;
</description></item><item><title>Learning About GPUs Through Measuring Memory Bandwidth</title><link>https://www.evolvebenchmark.com/blog-posts/learning-about-gpus-through-measuring-memory-bandwidth</link><guid>https://www.evolvebenchmark.com/blog-posts/learning-about-gpus-through-measuring-memory-bandwidth</guid><description>&lt;h2 id=&quot;learning-about-gpus-through-measuring-memory-bandwidth&quot;&gt;Learning About GPUs Through Measuring Memory Bandwidth&lt;/h2&gt;
&lt;p&gt;At Traverse Research, we need to have a deep understanding of GPU performance to develop our benchmark, Evolve. Additionally, we sometimes do projects for very specific hardware where we need to know all the ins and outs of this hardware. One way we do this is by using microbenchmarks to measure specific parts of the GPU to get new insights. In this article, we will share what we learned from measuring the memory bandwidth of various GPUs. First we will be going over some background information about GPU hardware relating to loading from and storing to memory, then we will take a look at how our microbench is built, and finally we will look at some GPUs of which we measured the bandwidth and what we learned from that.&lt;/p&gt;
&lt;h4&gt;Background Information&lt;/h4&gt;
&lt;p&gt;Accessing memory on a GPU is quite a bit more complicated than on a CPU. In this section we will talk about different concepts that are handy to keep in mind when programming for a GPU.&lt;/p&gt;
&lt;h5&gt;Descriptors&lt;/h5&gt;
&lt;p&gt;Memory on a GPU is usually not directly accessed via a pointer like on a CPU. Although some hardware is capable of doing this, buffer and textures access usually happens via a descriptor. A descriptor is nothing more than a pointer with extra metadata to support more complex logic when fetching data. For example for a texture the hardware needs to know the resolution, swizzle pattern, format, number of mip levels, whether the texture uses MSAA, and more to be able to load from it. This is all encoded in the descriptor. How this descriptor is represented in binary is up to the hardware vendor and is thus something we generally cannot see directly. Buffers are also accessed via a descriptor but often only encode a pointer with a size. If you ever wondered how the hardware is able to return a default value when reading out of bounds, this is how.&lt;/p&gt;
&lt;h5&gt;Types of buffers&lt;/h5&gt;
&lt;p&gt;When talking about buffers there are a couple of distinctions we need to make between the various sorts, since each have their own advantages and disadvantages.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Byte Address Buffers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The most basic form is a Byte Address Buffer, or sometimes called the Raw Buffer, this type allows us to load any data type in the shader by passing a byte offset. However, that is not the complete story. GPUs are generally not able to access non-4-byte aligned data, so the byte offset in reality has to be a multiple of 4. Additionally, most hardware is able to load in data in chunks of 4, 8 and 16 bytes. This reduces the number of load requests. Some hardware is able to load these larger chunks from any 4-byte aligned address, but not all hardware is able to do so. Since Byte Address Buffers do not give any guarantees in terms of alignment the shader compiler may generate four 4-byte loads instead of a single 16-byte load.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Structured Buffers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Structured Buffers are a more strict version of the Byte Address Buffer. The graphics API requires the user to specify the size of the data type. Additionally, when not using bindless, a StructuredBuffer in the shader can only load one data type. These restrictions allow the driver and shader compiler to guarantee that data is aligned in an optimal way to allow for 8 and 16-byte load instructions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Typed Buffers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Typed buffers are a bit more special as they use some of the functionality of the texture units. This allows us to load from, for example, a RGBA8_UNORM buffer and let the hardware unpack it to a float4. If we were to use for example a raw buffer, we would need to use ALU instruction to do this conversion. Of course this is a nice advantage but using the texture units is not always free either. The general advice is to not use typed buffers unless you really need the extra ALU that would otherwise have been spent the data unpacking.&lt;/p&gt;
&lt;h4&gt;Texture Units&lt;/h4&gt;
&lt;p&gt;While buffer loads are fairly straightforward, texture loads can get quite complicated. What&#39;s interesting is that all this extra complexity can be implemented partially or entirely in software, but it is often implemented in hardware for performance. Although this bit of the hardware comes in many flavors, we tend to refer to this bit of the hardware as the &#39;texture units&#39;.&lt;/p&gt;
&lt;p&gt;Let&#39;s go through the complexities that the texture units do one step at a time. Firstly, a texture load can be 1D, 2D, 3D, have mip levels, be an array, be a cubemap, or a mix of these. For each of these formats a floating point coordinate needs to be converted to a texel coordinate. Secondly, there are the address mode sampler settings that decide whether UV coordinates need to be wrapped around, clamped, mirrored or if a border color needs to be used. Finally, the filtering mode also has an impact on the address calculation, as with linear and anisotropic filtering, more than one texel needs to be loaded. All this together can result in a lot of logic and texel loads, just imagine doing an anisotropic sample in the corner of a cubemap array texture.&lt;/p&gt;
&lt;p&gt;The texture units do more than only address calculation. When it loads a texel, it may also need to unpack it to a float32 value. For a format like RGBA8_UNORM this is four relatively simple byte to float conversions, but the format may also require an sRGB calculation or use a more complicated block compressed format. To give an example of such a format, in the block compressed formats of PC GPUs, BC1 till BC3, we find two 16-bit colors with a 4x4 matrix of 2-bit elements. Each element indicates how to blend the two 16-bit colors together to create the decompressed color. Finally, the texture unit blend all the loaded texels together which is then stored in the requested register for use by the shader.&lt;/p&gt;
&lt;h4&gt;Memory Hierarchy&lt;/h4&gt;
&lt;p&gt;Due to physical limitations like the speed of light it is impossible to have a large amount of VRAM like 16GiB be fast enough to keep up with the computational speed of a GPU. This is both a limitation in bandwidth and latency. To alleviate this problem caches are added in various places in the chip. These vary in size and speed as well as proximity to where ALU operations happen. The general rule of thumb is, the closer a cache is to where computation is happening, the faster and smaller it is. On most hardware we find at least 2 levels of cache (L1 and L2) as well as an instruction cache (I-Cache). AMD&#39;s RDNA4 hardware even has an L0, L1, L2, Infinity Cache, Instruction Cache, and Scalar Cache. The multiple levels of cache complement each other to create a good balance between size and performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Write-through vs write-back vs write-around&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Caches do impose an interesting problem when it comes to writing data, do you let the write go through the other caches to main memory, do you wait until the cache line is evicted, or do you skip the cache entirely? These different approaches are called &#39;write-through&#39;, &#39;write-back&#39;, and &#39;write-around&#39;. In general, we mostly find the write-back approach on GPUs. The advantage of this approach is something called &#39;write combining&#39;. This lets more stores accumulate on the same cache line so that the whole line can be written into the next level of the memory hierarchy all at once when the cache line is evicted instead of a small store every time a store happens.&lt;/p&gt;
&lt;p&gt;The caches in a GPU are really optimized for this type of spatially local memory access, where these bursts of memory can be read or written at the same time. When a shader accesses data in a very sparse pattern, the hardware can try to coalesce these memory requests where possible, but will at some point have to send out more requests than if the memory accesses were more spatially local.&lt;/p&gt;
&lt;p&gt;Some algorithms may require reading data generated by other threads in the same shader. A simple example of this is a BVH refit where you have a thread per node. Each thread traverses up the tree and would start working on the same data as its neighboring thread, except that we use atomics to kill the thread that got there first. That creates the guarantee that one thread is working on one node. What now needs to happen is that the thread needs to load the AABBs of the child node. However, one of these child node AABBs may been generated by a thread on the other side of the machine, and may still be stuck in an L1 cache we cannot access. To overcome this the &#39;globallycoherent&#39; keyword can be used. This keyword indicates that writes should be done using some kind of write-through or write-around method until the first cache that is accessible for all the cores on the GPU. This guarantees that even though the data was written by a core that doesn&#39;t share the same L1 cache, we can still read it from L2.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hiding Latency&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Even with caches, there will still be situations where data is not in cache and has to be fetched from main memory. To deal with this, the GPU can hold more threads in flight than it can execute at once. Instead of letting a compute unit idle, it just switches to a different wave of threads to execute while it waits for the memory request to finish. By letting a shader not use too many registers or groupshared memory, a compute unit can keep many of them in flight, increasing the chance of not idling.&lt;/p&gt;
&lt;p&gt;It is important to note though that having more waves of threads in flight on the GPU is not always better for performance. In either very ALU or memory bandwidth-intensive use cases it can cause cache trashing because the relatively small cache cannot absorb all the memory accesses from all the waves. To illustrate this, imagine a cache line getting loaded in, and by the time it is accessed again, it has already been evicted from the cache due to so many other memory accesses of other cache lines causing it to get evicted. If fewer waves are running at a time the cache line may not have been evicted by the memory accesses of another wave.&lt;/p&gt;

&lt;h4&gt;Designing a Microbenchmark&lt;/h4&gt;
&lt;p&gt;A bandwidth microbenchmark can be as simple as creating a large buffer, running a shader, and reading every value from it. The read value has to be used in some way, otherwise the compiler will optimize the read out. The easiest way to do this is simply to write the value to a buffer.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Version 1
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; dispatchThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_DispatchThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;    float4 value &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dispatchThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;    outputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dispatchThreadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; value&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, this is not quite a read bandwidth benchmark, since we are not only measuring the read, but also the write. Luckily, there are two things we can do to improve. First of we can write our data into a very small buffer so that it fits in our nearest cache. That will turn the shader into this.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Version 2, write to tiny buffer
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; dispatchThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_DispatchThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_GroupThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;    float4 value &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;dispatchThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;    outputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupThreadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; value&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Secondly, we can amortize the cost of all the overhead (like dispatching the shader, spawning threads, writing to memory, etc) by performing many reads using a loop. Additionally, we can unroll the loop to amortize the cost of the loop itself.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Version 3, amortize the overhead by performing many reads
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c&quot;&gt;GROUP_SIZE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;256&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_GroupID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_GroupThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;    
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; groupId &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;    float4 acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;10&lt;/span&gt;    
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;11&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;12&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;13&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;14&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;16&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;17&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;18&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;20&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;21&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;22&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;24&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;25&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;26&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;27&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;28&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;29&lt;/span&gt;    outputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupThreadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; acc&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;30&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Caches vs VRAM&lt;/h5&gt;
&lt;p&gt;The next problem we run into is that our measured read bandwidth of main memory is shooting through the roof. This is because we are reading data from the cache instead of VRAM! This is something we would also like to measure, but let’s first fix the VRAM measurement.&lt;/p&gt;
&lt;p&gt;Avoiding constant cache hits is fairly simple. If we let every group start at a different part in the buffer, the chance that we are loading a chunk of memory that is in cache becomes nearly 0.&lt;/p&gt;
&lt;p&gt;When measuring memory bandwidth, we need to take into account that there are various caches between the compute units and the main memory. Luckily, by measuring with increasingly larger buffers and carefully picking the location we load from, we can trigger as many cache misses as possible. When using a tiny buffer, we will practically be guaranteed to hit the nearest cache, and with a large buffer, like 1 GiB, we will always load from VRAM.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; Version 4, Spread the groups to prevent cache hits
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-import syntax-define syntax-c&quot;&gt;#define&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt; &lt;span class=&quot;syntax-entity syntax-name syntax-constant syntax-preprocessor syntax-c&quot;&gt;GROUP_SIZE&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-preprocessor syntax-macro syntax-c&quot;&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;256&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-brackets syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;numthreads&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;main&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;5&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_GroupID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;6&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupThreadId&lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;SV_GroupThreadID&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;8&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; On the CPU:
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;9&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; uint bufferChunks = valueCount / (GROUP_SIZE * LOADS_PER_LOOP)
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;10&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; uint chunkStride = (loopCount + bufferChunks + 1) * (GROUP_SIZE * LOADS_PER_LOOP);
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;12&lt;/span&gt;    &lt;span class=&quot;syntax-support syntax-type syntax-sys-types syntax-c&quot;&gt;uint&lt;/span&gt; groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupId &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;chunkStride&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;%&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;14&lt;/span&gt;    float4 acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;15&lt;/span&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;16&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;17&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;18&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;19&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;20&lt;/span&gt;        cc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;21&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;22&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;23&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;24&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;25&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;26&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;27&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;28&lt;/span&gt;        acc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; inputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; groupThreadId&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;29&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; GROUP_SIZE&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;30&lt;/span&gt;        groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;groupLocation &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; constants&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-member syntax-c&quot;&gt;valueCount&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; groupLocation&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;31&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;32&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;33&lt;/span&gt;    outputBuffer&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;groupThreadId&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; acc&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;code-line-number&quot; aria-hidden=&quot;true&quot;&gt;34&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another advantage of this version of the microbenchmark is that we can have any number of threads and any-sized buffer. This allows us to both test with many more threads than elements in the buffer to get a more stable timing, as well as use tiny buffers to measure the size and speed of the caches.&lt;/p&gt;
&lt;h5&gt;Textures vs Buffers&lt;/h5&gt;
&lt;p&gt;While at first sight, loading from a storage texture or a buffer seems like almost the same operation, they might be handled very differently in hardware. By testing both textures and buffers, we can find these differences. Additionally, we use a mix of buffers and textures to see if the given GPU has a ratio where bandwidth is highest.&lt;/p&gt;
&lt;h5&gt;Stores vs Loads&lt;/h5&gt;
&lt;p&gt;Stores and loads can be handled quite differently in hardware, so this is an interesting thing for us to measure as well. Luckily, we need very few modifications to our benchmark to achieve this. We simply remove our dummy write at the end, replace all our reads with writes, and increment the accumulator by 1 every store to prevent the compiler from trying to optimize something we don’t want it to.&lt;/p&gt;
&lt;h5&gt;Element Formats&lt;/h5&gt;
&lt;p&gt;When loading and storing, we might be bottlenecked by the number of bytes that we can move over the memory bus, but can also be bottlenecked by the number of loads and stores we can issue. By using 4, 8, 12, and 16 byte element sizes, we can find these limitations. We implemented this in the shader using a simple macro for the data type.&lt;/p&gt;
&lt;h4&gt;‍&lt;/h4&gt;

&lt;h4&gt;Findings&lt;/h4&gt;
&lt;p&gt;Running the microbenchmarks on various hardware has been very insightful. From learning various quirks in different chips, to finding surprising bottlenecks in our microbenchmark.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT NOTE:&lt;/strong&gt; These benchmarks are not comparisons as to which GPU is ‘better’. They are just measurements to learn about the different GPU architectures. For this reason, we have opted to use GPUs from different price brackets and power targets. We are not interested in ‘this number is higher on this GPU’, but more interested in what patterns are slow or fast on a given piece of hardware. If you are interested in comparing the real-world performance of GPUs, the Evolve benchmark suite is great for this!&lt;/p&gt;
&lt;h5&gt;Qualcomm Adreno 740 (Meta Quest 3)&lt;/h5&gt;
&lt;p&gt;Let’s start off with a mobile chip, the Qualcomm Adreno 740 in the Meta Quest 3. This GPU has 12 Shader Processors, which each consist of 2 micro Shader Processors. We find a block of 1 KiB L1 cache per micro Shader Processor. Additionally, there is a block of shared L2 cache, and then further on a system level cache, which is usually shared with the CPU. From our measurements we see drop offs in read bandwidth after 128 KiB, 1MiB, and 8 MiB. For the write bandwidth we see clear drop off in performance at 512 KiB, but further drop offs after that are not quite as clear.&lt;/p&gt;
&lt;p&gt;Other than differences in bandwidth between the caches we found some interesting data about using buffers vs textures. We found it to be fairly common for there to be a difference in bandwidth for data in cache across many GPUs. However, on the Quest 3 we found a significant bandwidth difference in main memory. It is significant enough that, when designing software specifically for this hardware, it would be worth it to replace large buffers with textures to achieve a near 3x bandwidth improvement from around 46 GiB/s to over 130 GiB/s.&lt;/p&gt;
&lt;p&gt;Also interesting to note is that we hit the highest VRAM bandwidth of 143 GiB/s when we mix in 25% buffer loads. Additionally, we can see that using buffers is fine as long as we manage to hit the cache. When writing code we would recommend to use whichever resource type is most natural, but when optimizing for the Meta Quest 3 you might want to replace specific large buffers with textures to get that higher bandwidth.&lt;/p&gt;
&lt;p&gt;Finally it has to be noted that the memory bandwidth of the Adreno 740 on the Quest 3 is much lower than the other GPUs we tested for this article. This is because this GPU is designed for a much lower power budget. Increasing the memory bandwidth is definitely possible but not without sacrificing battery life. Instead Qualcomm, like other mobile GPU vendors, uses techniques like tile based rendering to reach good performance while minimizing memory traffic.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/meta-quest-3-read-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Read Bandwidth (GiB/s) on the Meta Quest 3 (Qualcomm Adreno 740)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/meta-quest-3-write-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Write Bandwidth (GiB/s) on the Meta Quest 3 (Qualcomm Adreno 740)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h5&gt;AMD Radeon RX 9070 XT&lt;/h5&gt;
&lt;p&gt;AMD has excellent documentation on what their hardware looks like. With the introduction of the RDNA architecture they released a white paper detailing the major differences, as well as releasing slides with what is new with each new iteration of their architecture. On top of that we also get a fully documented ISA full of little hardware details.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1030pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/full-overview.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;An overview of the AMD Radeon RX 9070 XT with the RDNA 4 architecture&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;An AMD RDNA 4 GPU is built out of a various number of Shader Engines. Each Shader Engine consists of two Shader Arrays, and each Shader Array contains some Work Group Processors. In the case of the Radeon RX 9070 XT there are a total of 4 Shader Engines with 4 Work Group Processors per Shader Array. The Work Group Processor consists of 4 SIMDs divided into two Dual Compute Units. In a Work Group Processor we find 16KiB of scalar cache, 32 KiB of instruction cache, 128 KiB for groupshared memory, as well as two blocks of 32 KiB L0 cache, one for each Dual Compute Unit. Per Shader Array we find 256 KiB of L1 cache. This cache is also used for coherency gathering the load/store requests before moving on to the 8 MiB of L2 cache. Finally the RX 9070 XT has 64 MiB of infinity cache, which on RDNA 3 was on separate chiplets, but it is back on a monolith die in RDNA 4. The sizes of the caches can vary from GPU to GPU though, for example the Radeon RX 9060 XT has 4 MiB of L2 cache and 32 MiB of Infinity Cache.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:2462pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/work-group-processor.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A zoomed in view of an RDNA 4 Work Group Processor&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;When benchmarking the Radeon RX 9070 XT, we ran into an interesting problem. When measuring the bandwidth, we noticed a significant difference in performance when loading floating-point values vs integers. This had us quite confused since, from a hardware perspective, it should not matter whether you load an integer or a float.&lt;/p&gt;
&lt;p&gt;It turns out we were running into an ALU bottleneck in our bandwidth benchmark! Just performing &lt;em&gt;one&lt;/em&gt; integer multiplication per load was enough to be a bottleneck. When we replaced our dummy multiply op with an addition, we were able to measure the same bandwidth for both integer and floating point values.&lt;/p&gt;
&lt;p&gt;I suppose we did not expect AMD’s L0 cache to be &lt;em&gt;that&lt;/em&gt; fast. We managed to measure it at nearly 20 TiB/s when loading from a buffer. When loading from a storage texture, we only managed to reach 11 TiB/s.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/amd-rx-9070-xt-read-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Read Bandwidth (GiB/s) on the AMD Radeon RX 9070 XT&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/amd-rx-9070-xt-write-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Write Bandwidth (GiB/s) on the AMD Radeon RX 9070 XT&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h5&gt;Intel Arc B580&lt;/h5&gt;
&lt;p&gt;Battlemage is Intel&#39;s second discrete GPU architecture. When it was announced with the introduction of the Intel Arc B580, they released some diagrams describing what their new GPU looks like under the hood, giving us some interesting insights.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:2343pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/full-overview-3.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;An overview of the Intel Arc B580 GPU architecture&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The Battlemage GPU architecture is built out of Render Slices, 5 in the case of the B580. Each Render Slice contains 4 Xe Cores as well as 4 Ray Tracing Units, 4 Samplers and some blocks for rasterization. Inside each Xe Core we find 8 Xe2 Vector Engines, 96 KiB of instruction cache and 256 KiB of L1 Cache. The L1 block is also used as groupshared memory where the block can be partitioned as needed. The main block of L2 cache is 18 MiB in size.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-center w-richtext-figure-type-image&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/render-slice.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A zoomed in view of an Intel Arc B580 Render Slice&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The B580 surprised us when comparing 4-byte vs 16-byte loads when loading from textures vs buffers. We found that for buffers, bandwidth goes down for larger data types, while for textures, it goes up for larger data types. This would suggest that data paths via buffers and textures, for the caches at least, seem to be optimized for different use cases.&lt;/p&gt;
&lt;p&gt;A second interesting find is that bandwidth is lower for 12-byte elements. This is likely because it is being split into two separate memory transactions, limiting the bandwidth.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/intel-arc-b580-read-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Read Bandwidth (GiB/s) on the Intel Arc B580&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/intel-arc-b580-write-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Write Bandwidth (GiB/s) on the Intel Arc B580&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h5&gt;NVIDIA GeForce RTX 5070 Ti&lt;/h5&gt;
&lt;p&gt;NVIDIA&#39;s Blackwell architecture is by far the largest scaled of all all the current generation GPUs. When the new architecture was released they also released a white paper describing all the changes as well as some very useful diagrams. The diagram they released is of their full GB202 chip, of which a slightly cut down version is found in the GeForce RTX 5090, likely due to binning.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:3039pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/full-overview-2.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;An overview of the NVIDIA Blackwell Architecture&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-floatright w-richtext-figure-type-image&quot; style=&quot;max-width:500px&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/streaming-multiprocessor.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;A zoomed in view of the Blackwell Streaming Multiprocessor (SM)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The NVIDIA GeForce RTX 5070 Ti we tested uses the same architecture but a smaller version of the chip. On chip we find 6 Graphics Processing Clusters (GPC), 35 Texture Processing Units (TPC), and 70 Shader Multiprocessors (SM). On the GPU we find 48 MiB of L2 cache, and as we zoom into the SM we find 128 KiB of L1 cache on each of them. The L1 block is also used for groupshared memory and can be split up with 10 different configurations ranging from 0 KiB to 228 KiB used for groupshared.&lt;/p&gt;
&lt;p&gt;In benchmarking the RTX 5070 Ti we found that many writes to the same small piece of memory causes a large bottleneck reducing performance massively. To be fair, this is not a use case with any real world computation uses, but it&#39;s interesting to see and makes us wonder what exactly is happening in the hardware that causes this.&lt;/p&gt;
&lt;p&gt;Another interesting find is that we saw a large decrease in read bandwidth when reading a uint1 from a buffer vs a float1. So far we found on AMD&#39;s Radeon RX 9070 XT that integer multiplies caused an ALU bottleneck. This case is different, as it&#39;s only the buffer loads that are affected, not the texture loads. We reran the microbench but got the same results back. So far we have not been able to explain why there is a difference in performance in this specific case.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/nvidia-rtx-5070-ti-read-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Read Bandwidth (GiB/s) on the NVIDIA GeForce RTX 5070 Ti&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1220pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/nvidia-rtx-5070-ti-write-bandwidth-charts.avif&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;Write Bandwidth (GiB/s) on the NVIDIA GeForce RTX 5070 Ti&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;With our new bandwidth microbenchmark we are just scratching the surface of what we can discover. So far we have been able to find that some paths can be faster on certain hardware like using textures vs buffers on the Meta Quest 3, or that loading 12-byte elements on Intel doesn&#39;t do well on the caches closest to the shader cores. However, we also found patterns that still remain a mystery to us. As we further develop our microbenchmarks we hope to uncover more insights on the hardware.&lt;/p&gt;
&lt;p&gt;Generally, we don&#39;t let one pattern being slower or faster on one specific vendor affect the way we write code, as we don&#39;t want to favor one GPU vendor over another. However, the common patterns are certainly helpful. Additionally, for projects targeting specific hardware that are not Evolve related, this type of data can helps us to squeeze every bit of performance out.&lt;/p&gt;
&lt;p&gt;We already have a lot of ideas for more microbenchmarks we want to build and try out. When we find some more interesting results we will share them with another blog posts so keep your eyes out for that!&lt;/p&gt;
&lt;p&gt;‍&lt;/p&gt;
&lt;p&gt;Sources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://chipsandcheese.com/p/inside-snapdragon-8-gen-1s-igpu-adreno-gets-big&quot;&gt;https://chipsandcheese.com/p/inside-snapdragon-8-gen-1s-igpu-adreno-gets-big&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.techpowerup.com/gpu-specs/radeon-rx-9070-xt.c4229&quot;&gt;https://www.techpowerup.com/gpu-specs/radeon-rx-9070-xt.c4229&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna4-instruction-set-architecture.pdf&quot;&gt;https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna4-instruction-set-architecture.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.techpowerup.com/review/amd-radeon-rx-9070-series-technical-deep-dive/3.html&quot;&gt;https://www.techpowerup.com/review/amd-radeon-rx-9070-series-technical-deep-dive/3.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://images.nvidia.com/aem-dam/Solutions/geforce/blackwell/nvidia-rtx-blackwell-gpu-architecture.pdf&quot;&gt;https://images.nvidia.com/aem-dam/Solutions/geforce/blackwell/nvidia-rtx-blackwell-gpu-architecture.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://download.intel.com/newsroom/2024/client-computing/Intel-Arc-B580-B570-Media-Deck.pdf&quot;&gt;https://download.intel.com/newsroom/2024/client-computing/Intel-Arc-B580-B570-Media-Deck.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description></item><item><title>Ray tracing animated crowds</title><link>https://www.evolvebenchmark.com/blog-posts/ray-tracing-animated-crowds</link><guid>https://www.evolvebenchmark.com/blog-posts/ray-tracing-animated-crowds</guid><description>&lt;p&gt;Animating large crowds in a ray-traced environment on the GPU&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/01.png.webp&quot; alt=&quot;A crowd in the Breda framework from our GDC demo&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A crowd in the Breda framework from our GDC demo.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-it-takes-to-render-crowds&quot;&gt;What it takes to render crowds&lt;/h2&gt;
&lt;p&gt;Crowd simulations have been common in games for a while now, but they usually only had to work for rasterization. Now that ray tracing is becoming more popular, new challenges have appeared. This blog post covers how we implemented animations and crowds in our testing framework “Breda” here at &lt;a href=&quot;https://traverse.nl&quot;&gt;Traverse Research&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are various challenges that need to be tackled while implementing such a system. First of all, the large number of vertex animations means we need an optimal GPU implementation to update all the meshes into their animated poses. Since these crowds will be used in both our path tracer and hybrid renderer, we also must take acceleration structure building into account for the animated meshes. These require a persistent set of vertices in their animated pose, which means that we will need a duplicate vertex buffer for each animated instance!&lt;/p&gt;
&lt;p&gt;Other than that we should keep in mind that we’ll need motion vectors for the animated geometry, and ideally have the ability to reuse as much of the animation state as possible to make the crowds perform well.&lt;/p&gt;
&lt;h2 id=&quot;our-animation-system&quot;&gt;Our animation system&lt;/h2&gt;
&lt;p&gt;Our animation system consists of a few basic types and concepts. We import most of our animation data from glTF, and store it in our own asset format. This format still closely resembles glTF.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Animations&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An animation in our engine is defined as a set of channels, where each channel has an output type (translation, rotation or scale). A channel defines the values for that output type for each key time-point in the animation. Every channel has an output index.&lt;/p&gt;
&lt;p&gt;We apply animations to animation samples to sample one specific time point for that animation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Animation Samples&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Animation samples contain the state of an animation for one time-point. The channels in an animation directly write these values into the animation sample. A sample can contain an arbitrary number of outputs, where each output is the combination of a rotation, translation and scale. These can be combined into a transformation matrix later.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/02.png.webp&quot; alt=&quot;Reading the channels in an animation for a specific time point&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading the channels in an animation for a specific time point. The outputs are written to the animation sample.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Note that not all outputs in the sample have every type of channel. In this example, if we were to apply &lt;code&gt;output 1&lt;/code&gt; to a transform, we would only override the rotation and translation. We keep the scale from the original transform.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Skeletons&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A skeleton is defined by a set of bones that are parented together to form a hierarchy. Each bone has a default position, called the bind pose (or T-pose). Skeletons and their bones are in local space, because we will want to reuse the same skeleton for multiple meshes at different locations. This requires a conversion step from glTF, as that format defines them in world-space.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Skeleton Samples&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These are similar to animation samples, in the sense that they contain the state of the bones in a skeleton for a specific time-point. In fact, each skeleton sample contains an animation sample! When we animate a skeleton, we first apply the chosen animation to this animation sample. Each transform output of the animation sample corresponds to a bone, and we apply the rotation, translation and scale overrides to that bone. We now have a set of bones in their animated state.&lt;/p&gt;
&lt;p&gt;Next up we apply the hierarchy of the bones. This involves multiplying the bone transforms in the right order. E.g.: the hand bone is attached to the arm, which is attached to the shoulder.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/03.png.webp&quot; alt=&quot;Applying an animation sample to a skeleton&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Applying an animation sample to a skeleton, and parenting the bones.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vertex Buffers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now that we have the final position of each bone in our skeleton sample, we can apply the bones to the vertices in a vertex buffer. Because we need to build a bottom level acceleration structure, these vertices must be persistent. We can’t rely on the old rasterization trick of transforming the vertices during the draw calls.&lt;/p&gt;
&lt;p&gt;Actually, there is still one thing we have to do before we can apply the bones to the vertices. We must first undo the bind-pose by applying the inverse-bind-matrix (ibm) to each vertex. To understand why, you have to realize that the vertices in their bind pose are already transformed by the bones. If we were to directly apply the new bone position, we would essentially have applied that bone twice. The ibm of a bone will transform that bone back to the origin of the world. If we apply it to a vertex, then we are moving that vertex back towards the origin (0,0,0), which means the vertex position is now relative to the bone. At this point, we can apply the new position of our animated bones to the vertices, and they will correctly end up relative to the bones.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/04.png.webp&quot; alt=&quot;The inverse bind matrix for a bone&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The inverse bind matrix for a bone makes vertices relative to that bone.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the example above you can see this happening. If we move the foot of the skeleton, then we want to move the vertices in the leg along the long green vector (the transform of the bone). However, the small red vector was already applied (the bind pose of the bone). First we undo the bind pose, then we can apply the new bone position.&lt;/p&gt;
&lt;p&gt;In our implementation, we pre-apply the ibm to each bone after we chain the bones together.&lt;/p&gt;
&lt;p&gt;Now, applying the bones to the vertices is fairly straightforward. First we read the vertices from the bind-pose vertex buffer in a compute shader, and then we multiply them with the corresponding bones. Each vertex can define up to four bone indices, and a weight for each of those bones.&lt;/p&gt;
&lt;p&gt;Finally we write the animated vertex away to the animated vertex buffer.&lt;/p&gt;

&lt;h2 id=&quot;world-infrastructure-crowds&quot;&gt;World Infrastructure &amp;amp; Crowds&lt;/h2&gt;
&lt;p&gt;The previous section discussed what the animated data and update logic looks like, but it did not yet touch on how we actually set up these animations in the world. Most importantly, it did not involve anything about crowds yet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hierarchy&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Central in our framework is a hierarchy of nodes that all have a transform. These represent the objects in our world. Each node can have an animated vertex buffer attached to it, but a node’s transform can also be animated directly.&lt;/p&gt;
&lt;p&gt;Vertex buffers are not directly attached to nodes, but rather we attach a small container (we call them “meshes”) composed of references to a vertex buffer, skeleton sample, bottom level acceleration structure and materials. These “meshes” can be composed separately, and reused between multiple nodes.&lt;/p&gt;
&lt;p&gt;Attaching a mesh to a node means we update our draw calls and add an acceleration structure instance to our TLAS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Node Animation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As mentioned before, we can also directly animate nodes in the world. Think for example of rotating objects. We don’t have to apply vertex animation here, we just need to override a node’s transform in the hierarchy directly. This is why we can bind nodes in our hierarchy to a specific animation sample. We gather these, and apply the transforms in a compute pass.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Skeleton Parenting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We’d very much like it if our crowd actors could hold items in their hands, or wear hats. To do this, we should be able to parent nodes to bones in a skeleton. But this is a problem, because our skeletons are not actually a part of the hierarchy. Instead they are in local space so that we can reuse them. Fortunately this is simple to solve. We can provide nodes with a skeleton sample and bone index, and patch the node’s transform in a compute pass that reads and applies the right bone.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/05.png.webp&quot; alt=&quot;An example hierarchy with animated nodes and skeleton parenting&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;An example hierarchy that contains animated nodes, a mesh using skeletal animation and a node that is attached to a specific bone within a skeleton.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crowd Instancing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A hierarchical approach like this allows us to reuse a lot of animation, and forms the backbone of our crowd system. For example, we can decide to load a single skeleton, and animate it ten times at different time points. We can then apply those ten skeleton samples to 100 unique vertex buffers. Those 100 vertex buffers can be combined with different sets of materials to create many unique “meshes”. Each mesh can then be added to one or more nodes to create large crowds with limited animation updates.&lt;/p&gt;
&lt;p&gt;Because we need to make a full copy of the vertex buffer for each unique animated instance, reusing these also saves a lot of memory.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/06.png.webp&quot; alt=&quot;Instancing for crowds&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Instancing for crowds to limit the amount of skeleton and vertex updates, while still having lots of variation.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Double Buffering for motion vectors&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since we apply all sorts of graphics effects, we often need motion vectors so that we can retrieve temporal information from the previous frame. This goes for vertex animated geometry as well. Fortunately, we already need to keep a copy of the animated vertex data around for ray tracing. Double buffering the positions for those buffers is a small extra cost that allows us to then always retrieve the previous frames’ vertex positions for a specific mesh. Subtract those from the current positions, and you have your motion vectors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BLAS building and refitting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Every time we update a vertex buffer in the animation system, the bottom level acceleration structure that uses the buffer must also be updated. This can be done by either rebuilding it completely, or by performing a refit. If the changes in geometry are small enough, a refit is often cheaper and still gives good tracing performance. In our framework we rebuild once every N frames, and do refits for the rest. We do this because the BLAS quality deteriorates over time while refitting. A heuristic can be used here to determine when a rebuild should happen.&lt;/p&gt;
&lt;p&gt;The rebuilds and refits can be batched in most modern graphics APIs. This gives the driver the opportunity to limit the number of dispatches for better performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Other world stuff&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This sums up the main tasks in our world update regarding animation. Of course, there’s more engineering around it. Sorting the draw calls and setting up our TLAS requires extra work. We have some logic in place that ensures that animation updates always run at least once so that we never have any uninitialized state.&lt;/p&gt;
&lt;p&gt;Animating a crowd with many instances of the same mesh, but with different materials, also comes with some downsides. Draw call sorting depends on the material, so you’ll have to split up your instanced draw calls, and change pipeline state. BLASes require you to specify geometry flags that tell you whether a triangle is opaque or not, which means you’ll have to build new BLASes when the opacity of the materials between instances changes. Solving these is not in the scope of this blog post, but be warned that you will run into these!&lt;/p&gt;
&lt;h2 id=&quot;compute-on-the-gpu&quot;&gt;Compute on the GPU&lt;/h2&gt;
&lt;p&gt;Now that we have all the theory down, we are still left with a considerable amount of work that the GPU has to do. How do we implement this optimally?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Compute work&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Our animation system does a total of 7 compute dispatches, regardless of how many animated objects we have.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Read animation channels and write to animation samples.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply animation samples to bones.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply the skeleton hierarchy to the bones.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply the ibm to each bone, and calculate the normal matrix.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply bones to vertex buffers.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply animation samples to nodes.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Apply bones to nodes on the hierarchy.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Optimizing &amp;amp; bindless rendering&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The main optimization that we can do is to batch as much work as possible. Launching a compute pass per skeleton to update its bones would be bad for performance because it comes with some overhead. Furthermore, for small skeletons it would mean that a large portion of the threads would be idle.&lt;/p&gt;
&lt;p&gt;The same thing goes for all the other passes: we have many small buffers containing similar data. If all this data had been in a single large buffer, then a single update step would have been easy. Fortunately for us, we have a bindless rendering setup that allows us to dynamically access any buffer from the GPU. This means that we can launch a single compute dispatch that goes over all the buffers, and in turn all the data in those buffers.&lt;/p&gt;
&lt;p&gt;If you would like to know more about our bindless implementation, check out our blog post series by Darius Bouma on setting up a bindless renderer. &lt;a href=&quot;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&quot;&gt;https://blog.traverseresearch.nl/bindless-rendering-setup-afeb678d77fc&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, the technique I used to batch all these operations is by building a prefix sum. I’ve described a fast implementation of this algorithm in my previous blog post, which you can find here: &lt;a href=&quot;https://blog.traverseresearch.nl/fast-cdf-generation-on-the-gpu-for-light-picking-5c50b97c552b&quot;&gt;https://blog.traverseresearch.nl/fast-cdf-generation-on-the-gpu-for-light-picking-5c50b97c552b&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Quickly summarized, we want to know the total number of operations we have to perform. For example, if we have 50 skeletons in separate buffers with 10 bones each, then we have a total of 500 bones that need to be updated. We would need to launch a compute pass with 500 threads, each of which will update a single bone.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/blog/images/ray-tracing-animated-crowds/07.png.webp&quot; alt=&quot;Prefix sum over bone buffers&quot; /&gt;&lt;/p&gt;
&lt;p&gt;By calculating a prefix sum, we can use each thread’s global index to deduce the skeleton and bone index to update. This is done using a binary search in the prefix sum array. This array stores for each buffer how many elements came before it. For example, the first buffer has seen 0 elements. The second buffer has seen all elements in the first buffer, and so forth.&lt;/p&gt;
&lt;p&gt;We can repeat this prefix sum implementation for each pass in our animation system. It can be done for the animation channels, the bone buffers and any other data that is spread out over many small buffers.&lt;/p&gt;

&lt;h2 id=&quot;shortcomings-future-work&quot;&gt;Shortcomings / Future work&lt;/h2&gt;
&lt;p&gt;Overall we are very happy with the implementation we came up with, though there are a few shortcomings still which I’ll list here.&lt;/p&gt;
&lt;p&gt;Firstly, we don’t have access to the world space transforms of all our actors, because animations happen on the GPU. This was not a big problem for our demo, but in a more game-like scenario you’d quickly run into issues here. Proposed solutions are to mimic parts of the animation pipeline on the CPU depending on which transform you need to know, or to do a read-back (which would mean your transforms are always a frame behind).&lt;/p&gt;
&lt;p&gt;We also didn’t tackle animation blending or bone labeling. These were simply not a priority for us, but are on the backlog. Perhaps we’ll release a blog post in the future to cover these.&lt;/p&gt;
&lt;p&gt;Lastly, our crowds can still benefit from some changes. For example, adding LODs to not just the crowd actors, but also their skeletons and animations could improve performance. This was however not in scope for us at the time, as it would likely require a lot of manual tweaking by an artist. The same thing goes for adding more variation to the clothing worn and objects held by actors in the crowd. An interesting talk on these topics can be found here: &lt;a href=&quot;https://www.youtube.com/watch?v=Rz2cNWVLncI&quot;&gt;https://www.youtube.com/watch?v=Rz2cNWVLncI&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I hope this was helpful! Feel free to leave a comment if you have any questions, and I’ll do my best to answer them.&lt;/p&gt;
</description></item><item><title>Shader Execution Reordering Benchmarked</title><link>https://www.evolvebenchmark.com/blog-posts/shader-execution-reordering-benchmarked</link><guid>https://www.evolvebenchmark.com/blog-posts/shader-execution-reordering-benchmarked</guid><description>&lt;h2 id=&quot;shader-execution-reordering&quot;&gt;Shader Execution Reordering&lt;/h2&gt;
&lt;p&gt;We have seen a big transition in real-time graphics where more and more games use ray tracing and even path tracing. These rendering techniques often lead to workloads that are difficult to predict because of the highly divergent paths taken by light rays as they bounce through a scene. This could be a significant performance hit on the GPU because threads execute in groups that I will refer to as waves (called warps on NVIDIA, wavefronts on AMD, and subgroups on Intel), where generally all threads in a wave execute the same instruction in lock-step. Overhead occurs when control flow diverges because both paths of a branch must be executed sequentially. Aside from that, incoherent memory access patterns prevent optimal cache usage and memory coalescing.&lt;/p&gt;
&lt;p&gt;To improve execution and memory access coherence within waves, DirectX Raytracing (DXR) Tier 1.2 with Shader Model 6.9 introduces Shader Execution Reordering (SER). SER opens the opportunity for application developers to suggest how to dynamically regroup threads across waves based on user-defined coherence hints and/or information about the traced ray. The specification defines that this can only happen in a ray generation shader on a best-effort basis left to the implementation. This coherence hint should be the same value for workloads that are expected to take the same divergent path and load similar data so they can be grouped and executed within the same wave, potentially improving both execution and data access coherence.&lt;/p&gt;
&lt;h2 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h2&gt;
&lt;p&gt;The HitObject type has been added to store the context of a ray after traversal. It can replace calls to TraceRay() using HitObject::TraceRay() or subsume the results of a RayQuery using HitObject::FromRayQuery().&lt;/p&gt;
&lt;p&gt;There are three ways to indicate how threads may be reordered. The simplest way to request reordering is by passing a HitObject as an argument. In this case, the driver may use properties of the HitObject such as the hit position or hit group index to group similar threads and improve execution coherence.&lt;/p&gt;
&lt;p&gt;Additionally, MaybeReorderThread() can take an optional coherence hint. This coherence hint is defined as a user-provided uint value along with a bit count specifying how many bits should be considered for reordering, starting from the least significant bit. The most significant bits in this range have the highest reordering priority. It is also possible to provide only the coherence hint without passing a HitObject instance (&lt;a href=&quot;https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#maybereorderthread&quot;&gt;source&lt;/a&gt;).&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-js&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;MaybeReorderThread&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt; 
  &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitObject&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitOrMiss&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; 
  &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;CoherenceHint&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-comma syntax-js&quot;&gt;,&lt;/span&gt; 
  &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;NumCoherenceHintBitsFromLSB&lt;/span&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-statement syntax-js&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The specification however does not guarantee that MaybeReorderThread() will perform any reordering, the minimal allowed implementation is a no-op.&lt;/p&gt;
&lt;p&gt;The sample below demonstrates how to use the new HitObject for pipeline ray tracing. It demonstrates the separation of the tracing stage via HitObject::TraceRay(), followed by reordering and finally executing the necessary hit or miss shaders bound via the shader binding table using HitObject::Invoke().&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;dx&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;:&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitObject&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;hit&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-entity syntax-name syntax-label syntax-js&quot;&gt;HitObject&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-js&quot;&gt;:&lt;/span&gt;:&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;TraceRay&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-spread syntax-js&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-js&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;MaybeReorderThread&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-spread syntax-js&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-js&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitObject&lt;/span&gt;::&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;Invoke&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-spread syntax-js&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-js&quot;&gt;)&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This next sample showcases how to perform a ray query using the existing RayQuery API. A HitObject is created from the committed hit in the RayQuery, which is then used to invoke dx::MaybeReorderThread(). A HitObject created from a RayQuery has no associated shader table entry, so calling Invoke() has no effect.&lt;/p&gt;
&lt;pre class=&quot;code-block syntax-code&quot;&gt;&lt;code&gt;&lt;span class=&quot;syntax-source syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;RayQuery&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-relational syntax-js&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-constant syntax-js&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-relational syntax-js&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;rayQuery&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-statement syntax-js&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-variable syntax-other syntax-object syntax-js&quot;&gt;rayQuery&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-js&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-method syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;TraceRayInline&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-spread syntax-js&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-js&quot;&gt;)&lt;/span&gt;;

&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitObject&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;hit&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-js&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;HitObject&lt;/span&gt;::&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;FromRayQuery&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;rayQuery&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;;
&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-js&quot;&gt;dx&lt;/span&gt;::&lt;span class=&quot;syntax-meta syntax-function-call syntax-js&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-js&quot;&gt;MaybeReorderThread&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-js&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-js&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-spread syntax-js&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-js&quot;&gt;)&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that MaybeReorderThread() is only available in ray generation shaders, even when using RayQuery.&lt;/p&gt;

&lt;h2 id=&quot;experiments&quot;&gt;Experiments&lt;/h2&gt;
&lt;p&gt;We ran experiments on different GPUs to see what the impact of adding Shader Execution Reordering would be on our Pipeline Path Tracing benchmark. We compared no reordering against two approaches: reordering based on the material index, and reordering using a “properties mask” that encodes certain material properties such as alpha masking, whether the material follows a significantly different code path (e.g. water or terrain), and how many layers the material has. This properties mask is obtained through a buffer load using the material index.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:2000pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/reorder-keys-visualized-trans.webp.webp&quot;/&gt;&lt;/div&gt;&lt;figcaption&gt;The reorder keys, each visualized with a different color.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;We tested the following GPUs at the given driver versions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;NVIDIA GeForce RTX 4070 Ti at preview driver version 590.26.0&lt;/li&gt;
&lt;li&gt;AMD Radeon RX 9070 XT at driver version 32.0.21007&lt;/li&gt;
&lt;li&gt;Intel Arc 140V (MSI Claw 8AI+) at driver version 32.0.101.8136&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Scores shown below are the Raytracing scores from the Evolve pipeline path tracing benchmark runs with Shader Model 6.9 and HV 202x. &quot;No re-ordering&quot; means that HitObject::TraceRay() is called without MaybeReorderThread().&lt;/p&gt;
&lt;h3 id=&quot;nvidia&quot;&gt;&lt;strong&gt;NVIDIA&lt;/strong&gt;&lt;/h3&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1880pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/nvidia-timings-3.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;On the tested NVIDIA GPU it appears that reordering based on the material index seems to be the most effective.&lt;/p&gt;
&lt;p&gt;In Nsight Graphics the wave coherence of the various shader stages can be visualized, when comparing this between the various reordering strategies used it becomes visible that reordering has a significant effect on wave coherence in the ray closest hit and miss shaders.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:954pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/table-effects-of-reordering-nsight-narrow.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;In the example below, you can see that when not reordering, only 34% of the threads within the wave execute the same instructions. When reordering based on the material this became 97% which resulted in a massive performance improvement since the #1 stall was texture loads.&lt;/p&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1003pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/material-nsight.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h3 id=&quot;amd&quot;&gt;AMD&lt;/h3&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1876pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/amd-timings-3.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;On AMD GPUs no performance difference was noticeable between the tested experiments. This is expected since they mention that MaybeReorderThread() currently does not move threads (&lt;a href=&quot;https://www.amd.com/en/resources/support-articles/release-notes/RN-RAD-MS-AGILITY-SDK-25-10-07-01.html#:~:text=Limitation%3A%20%E2%80%9CMaybeReorderThreads%E2%80%9D%20does%20not%20move%20threads%C2%A0&quot;&gt;source&lt;/a&gt;).&lt;/p&gt;
&lt;h3 id=&quot;intel&quot;&gt;Intel&lt;/h3&gt;
&lt;figure class=&quot;w-richtext-align-fullwidth w-richtext-figure-type-image&quot; style=&quot;max-width:1874pxpx&quot;&gt;&lt;div&gt;&lt;img alt=&quot;&quot; loading=&quot;lazy&quot; src=&quot;/assets/images/intel-timings-3.webp&quot;/&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;On Intel GPUs, Shader Execution Reordering does not currently provide a performance improvement. The ray tracing score was slightly worse than when not re-ordering when using the material index as a coherence hint, and lowest when the properties mask was used.&lt;/p&gt;
</description></item></channel></rss>
