<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>kvark&apos;s dev blog</title>
    <description>The history of kvark&apos;s ideas and hacking experiments.
</description>
    <link>http://kvark.github.io/</link>
    <atom:link href="http://kvark.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 18 Sep 2026 16:15:01 +0000</pubDate>
    <lastBuildDate>Fri, 18 Sep 2026 16:15:01 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Starcom: remote terminal session commander</title>
        <description>&lt;p&gt;Following up on the desktop app adventure started by  &lt;a href=&quot;/application/2026/03/14/fileman.html&quot;&gt;FileMan&lt;/a&gt;, I want to tell you about &lt;a href=&quot;https://github.com/navigato-rs/starcom&quot;&gt;StarCom&lt;/a&gt;. I had a problem: having multiple machines at home connected to the network and doing useful work. How do I keep track of what every one of them is doing and how do I make it persistent? I had to learn &lt;a href=&quot;https://github.com/tmux/tmux/wiki&quot;&gt;tmux&lt;/a&gt;, and for a while it seemed to be the solution. Just having sessions working on those machines, connecting to them over ssh to check and direct the next steps. Soon, connecting over and over again (every time after inactivity) became tediuous, and fighting with the weirdness of tmux scrolling, copy-pasting, and pane management, started to hurt more. I wanted a solution that would do the same but reliable and without memorizing the key combos, while minimally disrupting the server side. Importantly, I figured that there is nothing wrong with tmux on the server, I just don’t like the client side. So I took my app stack and started building a client.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/navigato-rs/starcom/blob/main/etc/screenshot.png?raw=true&quot; alt=&quot;preview screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What’s so good about &lt;a href=&quot;https://github.com/kvark/fileman&quot;&gt;Starcom&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s built with &lt;a href=&quot;https://github.com/kvark/blade&quot;&gt;blade&lt;/a&gt;, &lt;a href=&quot;https://www.egui.rs/&quot;&gt;egui&lt;/a&gt;, and &lt;a href=&quot;https://github.com/rust-windowing/winit&quot;&gt;winit&lt;/a&gt;. Works and looks the same on all major platforms, while rendering is fully GPU accelerated.&lt;/li&gt;
  &lt;li&gt;It’s designed for compatibility. You can always ssh+tmux to existing sessions, the client has no lock-in. You can use it with any existing cluster infrastructure without risks, since there is no new server parts.&lt;/li&gt;
  &lt;li&gt;Workload agnostic. There is no agent integration at all, it works regardless of &lt;em&gt;what&lt;/em&gt; you are running remotely.&lt;/li&gt;
  &lt;li&gt;Doesn’t hog your battery. The refresh is only on content change, with a special UI indicator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I constantly have 3 to 8 sessions open, switching between them. There is a simple inactivity tab coloring that tells me when things are stalled. It goes without saying that it seamlessly reconnects the sessions and needs no hotkeys to manage the panels. It quickly became the daily driver.&lt;/p&gt;

&lt;p&gt;Apparently, there is now &lt;a href=&quot;https://herdr.dev/&quot;&gt;Herdr&lt;/a&gt; as a popular alternative. I haven’t tried yet, but it feels more bloated and misguided from the outside. As such, the ability to manage different remote machines only came &lt;a href=&quot;https://github.com/herdrdev/herdr/releases#release-v0.9.0&quot;&gt;2 weeks ago&lt;/a&gt; - something I considered a core function from the start at Starcom.&lt;/p&gt;

&lt;p&gt;One interesting development is forking &lt;a href=&quot;https://github.com/navigato-rs/sunset&quot;&gt;sunset&lt;/a&gt; - a pure Rust SSH stack - to bring it to a usable state. Today, both FileMan and Starcom are relying on this stack to avoid C dependencies. Crab is the way! All 3 repos are now a part of the new &lt;a href=&quot;https://github.com/navigato-rs&quot;&gt;navigator-rs&lt;/a&gt; organization.&lt;/p&gt;

&lt;p&gt;If you find it useful, shoot me an email. Development with LLM is not hard. The hard part is knowing exactly what we need to build, so ideas are much welcomed ;)&lt;/p&gt;
</description>
        <pubDate>Fri, 18 Sep 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/application/2026/09/18/starcom.html</link>
        <guid isPermaLink="true">http://kvark.github.io/application/2026/09/18/starcom.html</guid>
        
        
        <category>application</category>
        
      </item>
    
      <item>
        <title>Feeding the data monster</title>
        <description>&lt;p&gt;Prompted by presentations from &lt;a href=&quot;https://luma.com/wkolq6uc?tk=aUgmgA&quot;&gt;Humanity &amp;amp; AGI Summit 2026: Robotics for Future Civilization&lt;/a&gt;, I sketched out a few strategies to train the robots. I don’t do this at work and by no means express the opinions of my employer here. The distinct strategies on my list are presented in the order from more realistic to more distant fantasy.&lt;/p&gt;

&lt;p&gt;The lack of data is often named as one of the showstoppers for robotic intelligence. Solving it the same way as Tesla solved FSD doesn’t appear possible: there is little use of fully human-controlled robots outside of some very specific areas (like surgery). I’m a big fan of simulation, but I’m going to put it aside for the purpose of this investigation - it deserves its own post and I don’t want to share my day-job work here. Also skipping tele-op since it scales poorly and targets a specific robot only.&lt;/p&gt;

&lt;h1 id=&quot;public-internet-videos&quot;&gt;Public internet videos&lt;/h1&gt;

&lt;p&gt;This is the current approach for many AI startups, at least a starting point for Skild AI and Rhoda AI. It’s easy to find a lot of data but hard to extract value. The goal is to train a world model that understands physics and causality. It has to be smart enough to then translate to a bot, and that translation is the hard part of the approach. Once the big world model exists, we can distill it to a small VLA to run on the bot directly as the policy, or manage to run the model directly on the bot.&lt;/p&gt;

&lt;p&gt;Notably, it’s a giant model that is trained in an explicitly constructed loop: engineers gather more data, clean it up, throw it into the oven, get the updated model out, etc. This isn’t AGI by any means, since the training loop is heavily assisted by humans.&lt;/p&gt;

&lt;h1 id=&quot;augmented-vision&quot;&gt;Augmented vision&lt;/h1&gt;

&lt;p&gt;See &lt;a href=&quot;/ar/2026/07/12/ar-explosion.html&quot;&gt;my AR explosion post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once people start wearing glasses, an interesting opportunity arises for robotics. Imagine being paid for sharing your (anonymized) recording together with your voice annotations of what you are doing. Practically golden labelled data right there, very easy to collect. Once these glasses are popular, and the data collection pipeline exists, we can start training models directly on it with behavioral cloning. Of course, there is still a domain gap to cross between the glasses’ cameras with human hands to the concrete robotic eyes and hands, but it’s a much easier bridge to cross than the wild internet data.&lt;/p&gt;

&lt;h1 id=&quot;self-training&quot;&gt;Self-training&lt;/h1&gt;

&lt;p&gt;Now we are getting more into sci-fi territory. If the robot is failing to assemble the Rubik’s cube, maybe the best solution is not to gather more data, post-train, and re-export and re-deploy the model. This is way too slow of a development cycle, especially if we want a robot to be useful in novel situations. Colonizing Mars anyone?&lt;/p&gt;

&lt;p&gt;What if the robot instead could learn on the spot? As long as it can compute the rewards, and it has the necessary hardware to do backpropagation (assuming the network architectures and hardware are roughly the same as today), it can do real-life reinforcement learning. A robot would accumulate knowledge and skills as it operates, occasionally sharing its best findings with others or a central management entity.&lt;/p&gt;

&lt;p&gt;Admittedly, rewards are the hard part here, and I don’t have the magic bullet for it. I suspect the answer lies in following the early development of humans. Intrinsic motivation through curiosity, novelty, play, plus explicit feedback from a “parent”.&lt;/p&gt;

&lt;p&gt;Live learning is something that I find extremely important for agentic physical AI. Not many companies even attempt to solve this problem. Everybody is focused on giant offline models and inference-only devices. If that direction kicks off, I’m curious how it’s going to drive the hardware development for AI inference and training: GPUs are not very efficient at this today, they spend most of their energy moving data around.&lt;/p&gt;
</description>
        <pubDate>Sun, 12 Jul 2026 07:02:02 +0000</pubDate>
        <link>http://kvark.github.io/ai/robotics/2026/07/12/feeding-the-data-monster.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ai/robotics/2026/07/12/feeding-the-data-monster.html</guid>
        
        
        <category>ai</category>
        
        <category>robotics</category>
        
      </item>
    
      <item>
        <title>AR explosion</title>
        <description>&lt;p&gt;AR technology is going through a lot of turbulence. Meta is not releasing a successor to Quest 3 (and no longer selling it to businesses), despite it being a popular device and making VR/AR entertainment affordable for many people. It laid off 10% of Reality Labs recently. I think it’s the calm before the storm.&lt;/p&gt;

&lt;p&gt;My bet is that the world of personal assistant electronics will change once this device comes to market:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;glasses that look and work like normal prescription/sun glasses. Maybe a slightly thicker frame but nothing out of ordinary. Not the bulky batteries and embedded screens like many of the current AR glasses feature (looking at &lt;a href=&quot;https://www.rayneo.com/pages/rayneo-air-4-pro-launch-best-ar-glasses&quot;&gt;RayNeo Air&lt;/a&gt;). It has to be indistinguishable from non-AR glasses. People don’t want to scream “look, I’m wearing something fancy!” in public.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.uprtek.com/en/blogs/waveguide-display&quot;&gt;waveguide&lt;/a&gt;-based screen with at least 70 degrees FOV. Both eyes need to be able to see an overlay - this would support a lot of AR applications, like navigation or translation, including stereo/3D overlays. This sector is moving quickly and is ripe for new products. Magic Leap has not succeeded at the product but it helped to move the tech forward, and the new seeds will pick it up and flourish.&lt;/li&gt;
  &lt;li&gt;hold 1 full day of battery life. Let it charge at night. Meta’s Ray-Ban glasses last 4-8 hours, and that’s without a display, so there is still room for growth.&lt;/li&gt;
  &lt;li&gt;well supported, reliable, and polished UX. Just like there were touchscreens before the iPhone, it needs a certain level of quality to gain momentum.&lt;/li&gt;
  &lt;li&gt;affordable. Has to be below 1K, ideally within 500$ range to succeed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These glasses will change the world significantly. Every person would find something useful: taking notes, assisting in travel, tracking, coordinating with personal AI systems, entertainment, and more. In terms of time, the technologies I listed are all around the corner. I’d give it a year.&lt;/p&gt;

&lt;p&gt;The fun part is - once you have these beauties you wouldn’t need your phone any more! Talking to people, checking the maps, tracking tasks, taking photos - all these are better in your smart glasses. One less device to carry in your pocket, to occupy the arm with. Imagine the market disruption, imagine how Apple survives if it misses the AR train.&lt;/p&gt;
</description>
        <pubDate>Sun, 12 Jul 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ar/2026/07/12/ar-explosion.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ar/2026/07/12/ar-explosion.html</guid>
        
        
        <category>AR</category>
        
      </item>
    
      <item>
        <title>Tales from the inference land</title>
        <description>&lt;p&gt;The &lt;a href=&quot;https://kvark.github.io/ai/performance/2026/04/04/inference-arena.html&quot;&gt;previous post&lt;/a&gt; described my new local inference and training benchmark - &lt;a href=&quot;https://inferena.tech&quot;&gt;Inference Arena&lt;/a&gt;. We talked about the list of models, the platforms, and the metrics captured. Perhaps the most interesting part of that post was about the results, and I want to expand more on it here.&lt;/p&gt;

&lt;p&gt;Disclosure: I’m the author of &lt;a href=&quot;https://github.com/kvark/meganeura&quot;&gt;Meganeura&lt;/a&gt;, one of the tested frameworks. I’ve tried to keep the methodology honest — judge for yourself. Will announce properly in a separate post.&lt;/p&gt;

&lt;h2 id=&quot;apple&quot;&gt;Apple&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/inferena/LLM-Apple.png&quot; alt=&quot;Apple M3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The Apple garden shows a lot of variety in the results. While &lt;em&gt;PyTorch&lt;/em&gt; fully supports this platform, it’s based on &lt;a href=&quot;https://developer.apple.com/metal/pytorch/&quot;&gt;Metal Performance Shaders&lt;/a&gt;. It runs significantly slower than Apple’s &lt;em&gt;MLX&lt;/em&gt; framework, which is specifically designed for ML workloads. We aren’t talking 20% here - it’s more than 3x slower on LLM (Large Language Model), 10x slower on VLA (Vision-Language-Action), and 50x on SD (Stable Diffusion). That last number is likely very sensitive to batch size and is first on my list to re-evaluate. At the same time, &lt;em&gt;JAX&lt;/em&gt; is showing parity on Apple since it uses MLX under the hood. And &lt;em&gt;Llama.cpp&lt;/em&gt; (as “GGML” in the tables) is even faster than &lt;em&gt;MLX&lt;/em&gt; on the LLM bench, unlike its performance on Linux/CUDA where it’s 6x slower than &lt;em&gt;PyTorch&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;amd&quot;&gt;AMD&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/inferena/Radeon-RX-7900.svg&quot; alt=&quot;Radeon 7900 XT&quot; /&gt;&lt;/p&gt;

&lt;p&gt;My main machine is an &lt;a href=&quot;https://frame.work/laptop13&quot;&gt;AMD Framework 13&lt;/a&gt;, which supports ROCm. However, it’s missing from a mini-PC I have with Radeon 780M - a popular integrated GPU from 2024 (just 2 years ago). I didn’t include it in the results because one of the frameworks crashes the bench for good - need to look more into it.&lt;/p&gt;

&lt;p&gt;Performance-wise, there is a room for improvement in ROCm stack. It’s strongly yielding to &lt;em&gt;ONNX RT&lt;/em&gt; and &lt;em&gt;Meganeura&lt;/em&gt; on VLA (Vision-Language-Action models) and LLMs (Large Language Models) respectively. This rough state is not limited to APUs: it’s also seen on Radeon RX 7900 XT discreet GPU, which I just acquired for testing.&lt;/p&gt;

&lt;h2 id=&quot;pytorch-reach&quot;&gt;PyTorch reach&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/inferena/GeForce-5080-linux.png&quot; alt=&quot;GeForce 5080 RTX&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PyTorch&lt;/em&gt; is the king on NVIDIA/Linux: slightly faster than &lt;em&gt;ONNX RT&lt;/em&gt; (for inference), significantly faster than &lt;em&gt;Llama.cpp&lt;/em&gt;, and ahead of anything written in Rust so far.&lt;/p&gt;

&lt;p&gt;What I found annoying is to always think about capturing the CUDA graphs and compiling the model to get fast inference. CUDA-graphing is basically pre-recording all GPU commands into a command buffer that gets re-used. This saves CPU costs of dispatching the kernels, which are quite significant in Python. Compiling the model is a process of graph optimization: fusing the kernels, reordering operations. Coming from the graphics land this seems unnecessary: nobody in sane mind would do eager shader compilation and workload scheduling every single frame in a video game. We just create pipelines early (or on demand) and re-use.&lt;/p&gt;

&lt;p&gt;On other platforms, &lt;em&gt;PyTorch&lt;/em&gt; support is a hit and miss:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;On Windows, &lt;em&gt;PyTorch&lt;/em&gt; was not able to compile the model, since &lt;a href=&quot;https://openai.com/index/triton/&quot;&gt;Triton&lt;/a&gt; - the actual GPU optimizer (and a separate language, no less!) by OpenAI used as the backend - doesn’t support the windowed OS.&lt;/li&gt;
  &lt;li&gt;It didn’t work on Intel “Raptor Lake” iGPU (can be seen in the new &lt;a href=&quot;https://frame.work/laptop12&quot;&gt;Framework 12&lt;/a&gt;) at all for me via the XPU backend.&lt;/li&gt;
  &lt;li&gt;No GPU acceleration on AMD’s Radeon 780M as mentioned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/resource/inferena/platform-support.svg&quot; alt=&quot;Platform support&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;deployment-struggle&quot;&gt;Deployment struggle&lt;/h2&gt;

&lt;p&gt;The deployment story is worrisome. I didn’t manage to build a “requirements.txt” file that would naturally support &lt;em&gt;PyTorch&lt;/em&gt; on all backends that it cares about. It’s a bit of a mess with all python frameworks, e.g. &lt;em&gt;ONNX RT&lt;/em&gt;’s “onnxruntime-gpu” yields to “onnxruntime” (the CPU backend) if both are installed. My “.venv” folder for Windows/NVIDIA is 7Gb in size, with Torch taking 2/3 of that - let that sink in. It’s safe to assume that you’d use &lt;em&gt;PyTorch&lt;/em&gt; for development only and deploy to edge devices running inference on an exported model of sorts.&lt;/p&gt;

&lt;p&gt;In Rust ecosystem, deployment is easier: just grab a static executable and roll. The established frameworks are &lt;em&gt;Burn&lt;/em&gt; (32Mb executable) and &lt;em&gt;Candle&lt;/em&gt; (5Mb), with &lt;em&gt;Luminal&lt;/em&gt; (8Mb) and &lt;em&gt;Meganeura&lt;/em&gt; (16Mb) being dark horses. I benchmarked with correctness gating, and so far I haven’t gotten a good signal from the leaders. In the LLM test their output loss was too far from &lt;em&gt;PyTorch&lt;/em&gt;, which makes the actual timings less relevant. Area of improvement for the benchmark.&lt;/p&gt;

&lt;h2 id=&quot;magical-barriers&quot;&gt;Magical barriers&lt;/h2&gt;

&lt;p&gt;The synchronization model of CUDA is different from Vulkan. CUDA has streams where Vulkan has queues. However, within a stream CUDA considers every operation effectively depending on all the previous ones. I find it highly peculiar, since it’s a very defensive stance that would be a better fit for some WebGPU API, not the fight-for-every-millisecond world that CUDA is in. I suspected one of the two things is right: either the NVIDIA driver is able to omit those dependencies in practice by tracking all of the kernel-to-kernel dependencies. Or CUDA is just weird, and maybe we can get faster with Vulkan since we can carefully place split barriers as we deem necessary. Naive, but worth checking. So far, based on &lt;em&gt;Meganeura&lt;/em&gt; optimization process and the numbers reached, it appears to me that one can get pretty close with Vulkan. E.g. SD inference is practically on par with &lt;em&gt;PyTorch&lt;/em&gt;/CUDA, which means that even if the driver has magic, it still comes down to essentially an L1 or L2 cache flush where appropriate, and Vulkan barriers allow you to control that. Let this be a drop of optimism for my graphics engineer colleagues entering the AI era.&lt;/p&gt;
</description>
        <pubDate>Sun, 19 Apr 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ai/performance/2026/04/19/tales-from-the-inference-land.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ai/performance/2026/04/19/tales-from-the-inference-land.html</guid>
        
        
        <category>ai</category>
        
        <category>performance</category>
        
      </item>
    
      <item>
        <title>Inference Arena</title>
        <description>&lt;p&gt;Looking at the screen, seeing my free space dropping by 20 GB from entering a Python development NixOS shell. It carries a few hand-selected ML libraries: pytorch, torchvision, transformers, onnx, and some more. That’s a ton of code! I wonder how it fares against smaller and newer libraries, including those made with Rust language.&lt;/p&gt;

&lt;h1 id=&quot;infenera&quot;&gt;Infenera&lt;/h1&gt;

&lt;p&gt;Here comes &lt;a href=&quot;https://inferena.tech/&quot;&gt;Infenera&lt;/a&gt; (short for “Inference Arena”) - my benchmark of ML framework inference and training. We are considering 5 standard models to start with:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SmolLM2 as a language model (SLM, not LLM?)&lt;/li&gt;
  &lt;li&gt;SmolVLA as a vision-language model&lt;/li&gt;
  &lt;li&gt;StableDiffusion as a generative model&lt;/li&gt;
  &lt;li&gt;Resnet50 as a classification model&lt;/li&gt;
  &lt;li&gt;Wisper-tiny as a speech recognition model&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is to cover the ground for different types of ML components used in these models: transformers with different kinds of attention, diffusers, convolution layers, MLP, and so on. For each model, we are interested in the following aspects:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;time per forward propagation - measuring &lt;em&gt;inference&lt;/em&gt; throughput&lt;/li&gt;
  &lt;li&gt;end-to-end time - measuring &lt;em&gt;latency&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;time per backward propagation - measuring &lt;em&gt;training&lt;/em&gt; throughput&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ve got a collection of ML frameworks to test on:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;starting with portable giants like PyTorch, JAX, and ONNX RT&lt;/li&gt;
  &lt;li&gt;adding prominent small players like &lt;a href=&quot;https://github.com/ggml-org/ggml&quot;&gt;GGML&lt;/a&gt; with its &lt;em&gt;llama.cpp&lt;/em&gt; + &lt;em&gt;whisper.cpp&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;with modern Rust-based frameworks like &lt;a href=&quot;https://github.com/tracel-ai/burn&quot;&gt;Burn&lt;/a&gt;, &lt;a href=&quot;https://github.com/huggingface/candle&quot;&gt;Candle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;and even underground &lt;a href=&quot;https://github.com/luminal-ai/luminal&quot;&gt;Luminal&lt;/a&gt; and &lt;a href=&quot;https://github.com/kvark/meganeura&quot;&gt;Meganeura&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;concluding with platform-specific frameworks like Apple’s MLX&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To instill trust in the results, we check for the loss to match PyTorch in both forward and backward passes. Results are crossed out if the loss is above 10% - it’s considered that a candidate ML framework worked with a different model in these cases.&lt;/p&gt;

&lt;h1 id=&quot;results&quot;&gt;Results&lt;/h1&gt;

&lt;p&gt;Firstly, PyTorch is very solid in performance. It’s good all around - pretty sure nobody is going to be fired by choosing PyTorch any time soon.&lt;/p&gt;

&lt;p&gt;Secondly, the difference in performance can be astonishing. When working on games, I tend to think about relative milliseconds. When working with compression algorithms back in the day, even 0.1% was a significant improvement. Here in ML land, if your data misses the chance to be properly on-chip when accessed, you immediately see 2x, 5x, and sometimes 10x difference. The performance curve is extremely sharp and sensitive to optimization.&lt;/p&gt;

&lt;p&gt;Thirdly, Apple is doing pretty well with their MLX framework, beating PyTorch on home ground. They may be struggling with actual models, but the infrastructure to run and train them is solid.&lt;/p&gt;

&lt;p&gt;Lastly, ML is not as accessible as I assumed. For instance, the Framework 12 that I got some 6 months ago doesn’t get an accelerated XPU device in PyTorch. Another mini-PC I have with Radeon 780M doesn’t support ROCm. And we haven’t even tried any of the mobile devices yet!&lt;/p&gt;
</description>
        <pubDate>Sat, 04 Apr 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ai/performance/2026/04/04/inference-arena.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ai/performance/2026/04/04/inference-arena.html</guid>
        
        
        <category>ai</category>
        
        <category>performance</category>
        
      </item>
    
      <item>
        <title>Blade XR Asteroids</title>
        <description>&lt;p&gt;Just as Meta turns around and shifts its attention to AI, following basically everybody, I want to tell you about XR. As an owner of Quest 3S, I always wondered if it’s easy to build for this device. Now I know.&lt;/p&gt;

&lt;h2 id=&quot;blade-xr&quot;&gt;Blade XR&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/kvark/blade/tree/main/blade-graphics&quot;&gt;Blade&lt;/a&gt; is the GPU abstraction layer I’ve been toying with lately. Recently, I’ve integrated support for OpenXR to it, while testing a new example on the Quest 3S. Blade-graphics would allow XR to create a Vulkan instance if requested, and Blade-engine would know to render to both eyes. But that was just the beginning - I had to overcome a few more blockers on the way to this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/resource/blade-asteroids.jpg&quot; alt=&quot;asteroids screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;First, it’s the ray tracing part. While Quest 3S declares support for ray tracing, the visuals &lt;a href=&quot;https://github.com/kvark/blade/pull/301&quot;&gt;appeared concerning&lt;/a&gt;. And we can’t expect any reasonable performance from this chip. The most reliable solution is to rasterize, like the old times. To achieve that, I’ve split &lt;a href=&quot;https://github.com/kvark/blade/tree/main/blade-render&quot;&gt;blade-render&lt;/a&gt; rendering pipeline into separate rasterization and ray tracing parts. The goal is to support both while sharing as much WGSL code as possible, internally. Also, sharing the vertex definitions, assets, etc.&lt;/p&gt;

&lt;p&gt;The other problem is the Adreno GPU. The driver for it appeared to be very cranky, and would produce &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VK_ERROR_UNKNOWN&lt;/code&gt; when compiling any non-trivial pipeline. After hours of debugging, I concluded that it doesn’t like the inline uniform blocks - a feature of Vulkan I was unconditionally using for convenience of having uniform data without explicit buffer management. Lacking a smaller workaround, I had to add support for old-school uniform buffers, managed internally in a bit of a hacky way. Hopefully, this will be useful to diagnose other GPU/driver issues outside of Adreno.&lt;/p&gt;

&lt;p&gt;When these blockers were addressed, it was just a matter of implementing the logic. You are supposed to fly through an asteroid field and shoot them down. I hooked up the physics from &lt;a href=&quot;https://github.com/kvark/blade/tree/main/blade-engine&quot;&gt;blade-engine&lt;/a&gt; and got the movement, shooting, and collisions all handled. All of the objects in the scene are procedurally generated for simplicity. I had to have some particle effects (comets!), so I lifted this logic out of the “particle” example into its own crate and extended it to work with the rasterizing renderer.&lt;/p&gt;

&lt;p&gt;The end result (aka “Asteroids”) appears to be pretty fun! Easy to get carried away while testing :) See the &lt;a href=&quot;https://vimeo.com/1175802946?share=copy&amp;amp;fl=sv&amp;amp;fe=ci&quot;&gt;full video&lt;/a&gt;. Hoping to release all changes on crates in the coming days.&lt;/p&gt;
</description>
        <pubDate>Sat, 21 Mar 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/blade/xr/2026/03/21/blade-xr-asteroids.html</link>
        <guid isPermaLink="true">http://kvark.github.io/blade/xr/2026/03/21/blade-xr-asteroids.html</guid>
        
        
        <category>blade</category>
        
        <category>xr</category>
        
      </item>
    
      <item>
        <title>File Manager</title>
        <description>&lt;p&gt;Time to dig up all of the projects and ideas that we didn’t do mostly because of the lack of spare time. I’ve just released &lt;a href=&quot;https://github.com/kvark/fileman/releases/tag/v0.1&quot;&gt;fileman-0.1&lt;/a&gt; - the first version of my lean file manager.
Ever since the time I worked with Windows and used a golden combo of &lt;a href=&quot;https://www.ghisler.com/&quot;&gt;TotalCommander&lt;/a&gt; + &lt;a href=&quot;https://www.irfanview.com/&quot;&gt;IrfanView&lt;/a&gt;, I’ve been searching for anything even close in Unix/MacOS land. That feel of low latency, immediate response, and polish with robustness. No reason why we can’t have good things, we just need to build them.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/kvark/fileman/blob/main/etc/snapshots/tests/preview.png?raw=true&quot; alt=&quot;preview screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What’s so good about &lt;a href=&quot;https://github.com/kvark/fileman&quot;&gt;FileMan&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s built with &lt;a href=&quot;https://github.com/kvark/blade&quot;&gt;blade&lt;/a&gt;, &lt;a href=&quot;https://www.egui.rs/&quot;&gt;egui&lt;/a&gt;, and &lt;a href=&quot;https://github.com/rust-windowing/winit&quot;&gt;winit&lt;/a&gt;. Works and looks the same on all major platforms, while rendering is fully GPU accelerated.&lt;/li&gt;
  &lt;li&gt;It’s designed for speed and low latency. You can enter a multi-gigabyte archive, navigate it as a folder, preview image files on the way. No operation will be blocking, and we’ll see the contents as soon as they asynchronously load.&lt;/li&gt;
  &lt;li&gt;Doesn’t hog your battery. The refresh is only on content change, with a special UI indicator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’m trying to actively use the other panel for things like help, preview, or edits, which is different from the TotalCmd approach. This is to minimize popups and keep everything together. When in preview mode, we load a small chunk to display, while the rest of the file is loading asynchronously. There is syntax highlighting for popular text formats, and image previewing.&lt;/p&gt;

&lt;p&gt;Overall, it’s a small program that is meant to help you navigate your drive easily. I’m sick of systems that ask “what do you want?” instead of giving you a menu. This tool puts you in control at all times.&lt;/p&gt;

&lt;p&gt;The tool is usable today. For future work, I’m looking at the following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;GLES support on Linux: extend platform support to older systems&lt;/li&gt;
  &lt;li&gt;tabs&lt;/li&gt;
  &lt;li&gt;faster image loading: load images at low-resolution instead of down-sampling&lt;/li&gt;
  &lt;li&gt;network drives&lt;/li&gt;
  &lt;li&gt;more polish&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you find it useful, shoot me an email. Development with LLM is not hard. The hard part is knowing exactly what we need to build, so ideas are much welcomed ;)&lt;/p&gt;
</description>
        <pubDate>Sat, 14 Mar 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/application/2026/03/14/fileman.html</link>
        <guid isPermaLink="true">http://kvark.github.io/application/2026/03/14/fileman.html</guid>
        
        
        <category>application</category>
        
      </item>
    
      <item>
        <title>AGENTS.md doesn&apos;t make sense</title>
        <description>&lt;p&gt;Trying to embrace the new era of AI-assisted engineering, and boy I’m worried! Mostly, about our inability to understand how the code really works, which is inevitable in the future where most of the code is produced by AI agents. A &lt;a href=&quot;https://www.modular.com/blog/the-claude-c-compiler-what-it-reveals-about-the-future-of-software&quot;&gt;counter-argument by Chris Lattner&lt;/a&gt; is that similar transition happened in the past when we stopped writing assembly. I think that worked because assembly translation is more mechanical and easier to test.&lt;/p&gt;

&lt;p&gt;Anyway, the story starts with an “AGENTS.md” file that the author places in the repo. I don’t think it makes any sense. Whatever you may want to put into it, there is a better place that is universal between AI and humans:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;description of what the project is about, and what it wants to be - should go into “README.md”&lt;/li&gt;
  &lt;li&gt;code style, conventions, and organization - should go into “CONTRIBUTING.md”&lt;/li&gt;
  &lt;li&gt;same goes for the testing procedures, dev environment, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What does make sense though, is a task or session specific permanent memory - “PLAN.md”. It would contain my asks, important considerations, and the current progress along the goal. I start my work with it and keep it updated, not adding under version control.&lt;/p&gt;
</description>
        <pubDate>Sun, 22 Feb 2026 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ai/2026/02/22/agents-nonsense.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ai/2026/02/22/agents-nonsense.html</guid>
        
        
        <category>AI</category>
        
      </item>
    
      <item>
        <title>UI failure</title>
        <description>&lt;p&gt;Most of the user interfaces suck. That’s a bit of a paradox: I can understand how technical debt can be swept under the rug, or how customer support can be a hit-and-miss, but user interfaces? That’s your main way of interaction with users, its business value appears to be disproportional of the investment and quality.&lt;/p&gt;

&lt;p&gt;In this article, I’m going to provide a few examples of bad interfaces and attempt to categorize the failures.&lt;/p&gt;

&lt;h1 id=&quot;discoverability&quot;&gt;Discoverability&lt;/h1&gt;

&lt;h2 id=&quot;everything-can-be-discovered-in-a-structured-way-without-relying-on-search&quot;&gt;Everything can be discovered in a structured way, without relying on search.&lt;/h2&gt;

&lt;p&gt;This tendency has been developinig for a couple of decades, and is now stronger then ever. Look at the evolution of Windows start menu between 95 and now:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/resource/ui/win95-start-menu.png&quot; alt=&quot;win-95&quot; /&gt;&lt;img src=&quot;/resource/ui/win11-start-menu.png&quot; alt=&quot;win-11&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We are seeinig the change of query paradigm of the user from “show me what we have” to “please find me this”. I believe it has a deep strategic basis. The change provides more freedom to the system in the areas that have business value:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;blurs the difference between local and cloud content, which makes cloud storage more accessible&lt;/li&gt;
  &lt;li&gt;allows filtering and re-ordering results. For example, if I’m searching for a browser, Windows may have opinions about what I can find.&lt;/li&gt;
  &lt;li&gt;includes advertising of stuff that I don’t (yet) have&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, this evolution shifted control from the user to the system. We no longer own things, and instead, every day we politely ask the corporations for bits we need.&lt;/p&gt;

&lt;h2 id=&quot;similar-things-should-be-close-together&quot;&gt;Similar things should be close together.&lt;/h2&gt;

&lt;p&gt;It took me around 30 minutes to locate this panel:
&lt;img src=&quot;/resource/ui/input-bar.png&quot; alt=&quot;windows input bar&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And even with that, I’m still not sure where exactly to see the full list of languages. The Windows setting interface is weird and arcane.&lt;/p&gt;

&lt;p&gt;Another poor example is Kaiser Permanente application:
&lt;img src=&quot;/resource/ui/Kaiser.jpg&quot; alt=&quot;Kaiser&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s a mix of different ways to get to similar functionality without a well defined structure.&lt;/p&gt;

&lt;h2 id=&quot;at-any-point-there-must-be-a-way-to-find-where-you-are&quot;&gt;At any point, there must be a way to find where you are.&lt;/h2&gt;

&lt;p&gt;A good example of this is Github API, which is generally very strong on the usability side. For navigation, there is always a row of categories at the top, with the current row selected by an underline:
&lt;img src=&quot;/resource/ui/github-menu.png&quot; alt=&quot;github menu&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;operation&quot;&gt;Operation&lt;/h1&gt;

&lt;h2 id=&quot;content-must-be-loaded-quickly&quot;&gt;Content must be loaded quickly.&lt;/h2&gt;

&lt;p&gt;User interfaces must be interactive. Any delay, even at the millisecond level, requires the user to stall while keeping attention. Any such delay must be minimized. This makes a difference between a slow IDE (like &lt;a href=&quot;https://www.jetbrains.com/clion/&quot;&gt;CLion&lt;/a&gt;) and a fast one (like &lt;a href=&quot;https://zed.dev/&quot;&gt;Zed&lt;/a&gt;). The aforementioned Kaiser Permanente app is a good anti-example: its operations frequently take seconds to resolve.&lt;/p&gt;

&lt;p&gt;I’d like to specifically point out Google mail here. It has been gradually getting worse since inception. Nowadays, it takes seconds to load, and operations have visible delays.&lt;/p&gt;

&lt;h2 id=&quot;navigation-must-be-detached-from-content&quot;&gt;Navigation must be detached from content.&lt;/h2&gt;

&lt;p&gt;Why did we end up with browsers that allow content creators to hijack the “Back” button? We have an organization like W3C not being able to protect the users from this harmful behavior. We are seeing browsers &lt;a href=&quot;https://www.pcmag.com/news/chrome-browser-to-stop-websites-abusing-the-back-button&quot;&gt;implementing mitigations&lt;/a&gt; against this behavior, but I’m shocked to see this API existing in the first place. The navigation panel is my tool as a user. It has no place in the API for the page I’m visiting.&lt;/p&gt;

&lt;p&gt;Similarly, sometimes when browsing Firefox on the phone, the webpage can make my controls inaccessible. Why are we allowing websites to render outside their client area?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/resource/ui/no-way-back.jpg&quot; alt=&quot;no way back&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;similar-links-should-function-similarly&quot;&gt;Similar links should function similarly.&lt;/h2&gt;

&lt;p&gt;The other side of “Similar things should be close together” - if I see similarly-styled controls, I don’t expect one to do a minor change to the page while the other to transfer me to another place. KP’s application is an example here: some links just direct me to the web view of the website, where I’m not even authenticated. It’s hard to know where a link would lead me.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/resource/ui/external-link.jpg&quot; alt=&quot;external link&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;content-must-not-be-changed-especially-the-locations-of-links&quot;&gt;Content must not be changed. Especially the locations of links.&lt;/h2&gt;

&lt;p&gt;In other words, all content changes must be initiated by the user. Every time Google is injecting that awful advertisement animation in the middle of the search results - a small kitten dies somewhere. Few things are more annoying than something messing with the content at the same time as you are interacting with it. The effect is similar to the navigation issue: content should be separated from the view, and the view is to be controlled by the user.&lt;/p&gt;

&lt;h2 id=&quot;every-user-action-needs-to-be-acknowledged&quot;&gt;Every user action needs to be acknowledged.&lt;/h2&gt;

&lt;p&gt;We need feedback at all times. The faster - the better. This experience is generally good when browsing the web on desktop: hovering the mouse makes some elements receive highlights, clicking makes the browser show the loading progress bar, etc.
Things are much worse on mobile: hovering gives you no signal, clicking may not necessarily register. Sometimes I’m not sure if I clicked too early and the original page is still loading, or it’s my request being loaded while the original page is displayed.&lt;/p&gt;

&lt;h1 id=&quot;good-examples&quot;&gt;Good examples&lt;/h1&gt;

&lt;p&gt;We have already mentioned GitHub. I suspect the usability aspect played a major role in becoming the de-facto platform for Open Source.&lt;/p&gt;

&lt;p&gt;Another great example is the Fastmail website. It’s very responsive (certainly lighter than GitHub), clean, has clear navigation, up to the point where you never notice it. I hope it stays like this and doesn’t fall under pressure from “modern” UI needs.&lt;/p&gt;

&lt;p&gt;Back in the day, a good OS UI example was shown by BeOS (or its resurrection - &lt;a href=&quot;https://www.haiku-os.org/&quot;&gt;Haiku&lt;/a&gt;). One of the architectural decisions was to have a dedicated thread per window for processing events. This, coupled with good kernel scheduling of threads, provided interfaces that were consistently responsive. At the time, neither Linux or Windows were as accessible.&lt;/p&gt;

&lt;p&gt;In addition to operational usability, it also had great discoverability. See, both UNIX and Windows filesystem structures are horrific! Why would I want my apps to be in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/share&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c:\Program File (x86)\&lt;/code&gt;? BeOS on the other hand, had a clear structure targeted at a single user. You could walk the filesystem and make sense of everything you need, without dedicated navigation apps/menus.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/resource/ui/beos5.png&quot; alt=&quot;BeOS 5&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Many user-facing systems around us have gross violations of basic principles of usability. I must have some very non-standard expectations here, seeing how those issues are tolerated.
I’m putting my wallet where my mouth is. I hope that we collectively exert more pressure (or provide a stronger signal) to the industry about how bad the situation with user interfaces is.
I hope to see lower latencies and less clutter. I hope to see the Web user agents (like Firefox) being more strict and opinionated about providing a good user experience.&lt;/p&gt;
</description>
        <pubDate>Wed, 13 Nov 2024 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ui/2024/11/13/ui-failure.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ui/2024/11/13/ui-failure.html</guid>
        
        
        <category>UI</category>
        
      </item>
    
      <item>
        <title>Environment map importance sampling</title>
        <description>&lt;p&gt;Environmental map is a common source of light in ray tracing applications. In bright day-time scenes, the ability to sample it efficiently is detrimental to reducing the noise. I want to share my experience with different ways of sampling. We assume that the environment is represented by a 2D image with pixels storing RGB floating point radiance. A value in a pixel represents the average radiance coming from any direction within the pixel area. The most redundant case for it being 1x1 white image. As for projection type, we are going to assume the Mercator projection.&lt;/p&gt;

&lt;p&gt;The goal of a sampling routine is to generate a pseudo-random direction in 3D, together with the probability of how this direction was picked. That’s the basis of importance sampling. If our probability is correlated to the amount of energy that is coming from that direction, the lighting will converge fast.&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sample&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// sampling direction&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// probability density in the unit of solid angle around the direction&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All the experiments were conducted within &lt;a href=&quot;https://github.com/kvark/blade&quot;&gt;blade&lt;/a&gt; engine. Shader code is in &lt;a href=&quot;https://gpuweb.github.io/gpuweb/wgsl/&quot;&gt;WGSL&lt;/a&gt; language.&lt;/p&gt;

&lt;h2 id=&quot;uniform&quot;&gt;Uniform&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/env-map/uniform-sampling.png&quot; alt=&quot;uniform&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The simplest way to sample a direction is uniform: every direction is equally probable. However, there is a catch. If we just pick random azimuth (from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-PI/2&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PI/2&lt;/code&gt;) and altitude, it’s not going to be uniform! Such a naive routine would oversample the poles of the sphere, because the amount of surface (on the sphere) changes as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cos(azimuth)&lt;/code&gt;. The solution to this is to consider the &lt;em&gt;height&lt;/em&gt; to be uniformly random. An interesting property of the sphere’s surface is that its area changes uniformly with the height: as we get closer to the poles, the radius decreases but the angle increases, which compensates it perfectly. I’ll leave the proof as an exercise to the reader - it’s fun, I highly recommend trying!&lt;/p&gt;

&lt;p&gt;Let’s see how it can be expressed in the code:&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Distance from the center to the projection of the direction on XY plane&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Density is such that the total integral across the sphere surface is 1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Uniform sampling is a great starting point, and also good for a backup plan when debugging more sophisticated algorithms. It’s easy to see where it fails: clear sky at day time would have most of the energy coming from the sun, and picking up that direction is not very likely with uniform sampling. That results in noise, lots of noise…&lt;/p&gt;

&lt;h2 id=&quot;important-incident-angle&quot;&gt;Important incident angle&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/env-map/cosine-sampling.png&quot; alt=&quot;cosine&quot; /&gt;&lt;/p&gt;

&lt;p&gt;At the end of the day, we are interested in maximizing the integral of the lighting equation, which is a product of the incoming light energy, its visibility, dot product with the surface normal, and the BRDF of the material.&lt;/p&gt;

&lt;p&gt;For importance sampling, we can consider some of these factors independently. The easiest one is the dot product of the light direction with the surface normal. We want the probability density to be proportional to the cosine of the angle between the light direction and the normal. Interestingly, this is the same cosine that broke the naive implementation of the uniform sampling! So all we need here is to choose the altitude naively, just sample in the space of the geometry surface.&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;altitude&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;altitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;azimuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;altitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Normal is expected to match the local Z, so `sin(altitude)` = `cos(angle_with_normal)`&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Division by PI is to normalize the integral across the hemisphere to 1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;altitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local_to_world_rotation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This sampling works great when the other factors are more or less uniform. E.g. when the sky is covered in clouds, and there is similar amount of energy coming from any direction. It would converge faster than the uniform sampling in this case, but not in the case with a single bright sun in the sky.&lt;/p&gt;

&lt;h2 id=&quot;important-light-energy&quot;&gt;Important light energy&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/resource/env-map/hdr_111_parking_space_2_prev.jpg&quot; alt=&quot;env map&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Roughly speaking, if we are to choose between different factors to sample with importance, we should pick one that contains the least entropy. Or the most variation. The energy encoded into the environment light texture often has a lot of variation, and it comes with floating point 16 or even 32 bits of precision. Thus, it makes sense to pick the important texels to sample from when choosing the direction.&lt;/p&gt;

&lt;p&gt;The task of importance sampling the texture belongs to the general class of discreet sampling algorithms. We have a set of texels, each with a corresponding energy value, and we want to importance sample them with probability that is proportional to the energy.&lt;/p&gt;

&lt;h3 id=&quot;hierarchical-descent&quot;&gt;Hierarchical descent&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/resource/env-map/hierarchy.png&quot; alt=&quot;env hierarchy&quot; /&gt;&lt;/p&gt;

&lt;p&gt;One way to do it is by starting with the highest mip level and then going through the mips down to the lowest level. Each step of the way we can randomly select one of the quadrants to follow. This method is used by the samples in RTXDI library from NVidia. I like it because it’s very intuitive, easy to imagine and reason about.&lt;/p&gt;

&lt;h4 id=&quot;preparation&quot;&gt;Preparation&lt;/h4&gt;

&lt;p&gt;In the preparation stage we need to build the mip chain. Each texel needs to contain 4 values: the total energy (or weight) of each of the quadrants in the 2x2 quad that this texel will be split into. The lowest mip is half the resolution of the original environmental map.&lt;/p&gt;

&lt;p&gt;My preparation routine is just averaging the energy at each level, issuing a compute dispatch. For the original map, it computes the luminance value of the color. There is a catch, however. Since texels may cover different solid angles, computing the average energy requires us to weight them based on that solid angle:&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;textureLoad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.target_level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;luma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LUMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.xyz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elevation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pixel&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_size&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relative_solid_angle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elevation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;luma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;relative_solid_angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_FP16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I used “Rgba16Float” texture to store the values, but I can think of a better way. We could store 3 of the weights normalized in a texture like “Rgb10A2Unorm”. The last weight could be reconstructed automatically, and that would reduce the texel size from 64 bits to 32 bits.&lt;/p&gt;

&lt;h4 id=&quot;traversal&quot;&gt;Traversal&lt;/h4&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;textureNumLevels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// texel coordinate&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// descend through the mip chain to find a concrete texel&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;textureLoad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// include the chosen probability&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// adjust for the texel&apos;s solid angle&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compute_texel_solid_angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map_equirect_uv_to_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The cost of this routine is proportional to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log(largest_dimension)&lt;/code&gt;. We are doing a texel fetch at each step.&lt;/p&gt;

&lt;h4 id=&quot;skip-self-occlusion&quot;&gt;Skip self-occlusion&lt;/h4&gt;

&lt;p&gt;This method can be improved by checking the texel areas against the surface normal. At each step of the descent, we can check if any of the texels are completely below the positive hemisphere, and reset their weight to 0. We could also lower the weight of texels base on how much they intersect with the lower hemisphere. This trick would make it much more likely to choose a direction that’s actually visible, at the cost of slightly more expensive descent.&lt;/p&gt;

&lt;h3 id=&quot;binary-search&quot;&gt;Binary search&lt;/h3&gt;

&lt;p&gt;Another way to approach this is more general. Given a set of texels with their weight, we can compute the &lt;a href=&quot;https://en.wikipedia.org/wiki/Prefix_sum&quot;&gt;prefix sums&lt;/a&gt; for each. The weight of a texel is energy per solid angle. This precomutation can be done on CPU or GPU, it results in a large array of data:&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EnvironmentTexel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;weight_prefix_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;coordinates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;storage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env_texels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EnvironmentTexel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, for sampling, we just need to binary search a random value into this array, given that the prefix sums are strictly increasing. This gives us the inverse of a Cumulative Distribution Function (CDF) of the given probability weights.&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;binary_search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env_texels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env_texels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env_texels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.weight_prefix_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.weight_prefix_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;map_equirect_uv_to_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;et&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.coordinates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The cost of this function is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log(width * height)&lt;/code&gt;, which is asymptotically the same as the previous method. There are numerous improvements that could be made here to increase the locality of access into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env_texels&lt;/code&gt; - something that the binary search is generally poor at.&lt;/p&gt;

&lt;h2 id=&quot;extra-tricks&quot;&gt;Extra tricks&lt;/h2&gt;

&lt;p&gt;One unfortunate property of importance sampling based on light intensity is that it’s surface independent. I.e. it’s equally happy to pick a sample pointing “downwards” from the surface. That means, on average, half of the samples can be occluded immediately (and thus not useful).&lt;/p&gt;

&lt;h3 id=&quot;multiple-samples&quot;&gt;Multiple samples&lt;/h3&gt;

&lt;p&gt;One easy trick to deal with that problem is to continue generating new samples for as long as they are occluded by the surface. This occlusion check is easy (we aren’t talking about a full shadow ray).&lt;/p&gt;

&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_samples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;estimate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_important_sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num_samples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// quickly check if it&apos;s self-occluded&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sm&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sample_environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sm&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;estimate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sm&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_samples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// now we can cast a real shadow ray&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;equal-area-mapping&quot;&gt;Equal area mapping&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/resource/env-map/a-The-octahedral-map-is-a-parametrization-of-the-sphere-onto-a-square-domain-of-n-n.png&quot; alt=&quot;equal area&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There are ways to map a square to a sphere’s surface with constant density.
See practical implementation &lt;a href=&quot;https://fileadmin.cs.lth.se/graphics/research/papers/2008/simdmapping/clarberg_simdmapping08_preprint.pdf&quot;&gt;by Clarberg&lt;/a&gt; for details.&lt;/p&gt;

&lt;p&gt;This mapping can be combined with another method (such as the binary search):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;pre-generate the 2D map (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;energy_map&lt;/code&gt;) where each texel contains the total energy of the area that the equal area maps this texel into.&lt;/li&gt;
  &lt;li&gt;pre-generate an acceleration structure (such as the prefix sum array) for this 2D map.&lt;/li&gt;
  &lt;li&gt;sample in 2 stages: first in precomputed energy map, then in the actual environment map.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pseudo-code of the sampling algorithm:&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MetaSample&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// generate an important sample of the precomputed energy map&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal_sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MetaSample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pick_meta_sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;energy_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Each texel maps to equal area of the sphere, divide by this area.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal_sample&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.pdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constant_texel_area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Random offset within the texel.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;textureSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;energy_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;equal_area_projection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;learn-the-map&quot;&gt;Learn the map&lt;/h3&gt;

&lt;p&gt;As it happens with problems that are difficult to solve algorithmically, we can resort to the mighty AI overlords. A neural network can learn the probability map of an environment map for a given surface orientation. Basically, the function:&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;probability&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surface_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;light_direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I don’t understand the magic behind learning these probabilities yet, but it’s possible. It can be seen as learning the environment map itself at low resolution. I.e. the goal is to roughly understand which areas are bright, and what can be seen from a given perspective.&lt;/p&gt;

&lt;h3 id=&quot;random-within&quot;&gt;Random within&lt;/h3&gt;

&lt;p&gt;If the sampling produced a texel coordinate of the environment map (as opposed to purely a direction), it’s not the end. We need to generate a light direction, and it can come from anywhere covered by this texel. In the redundant case of 1x1 map, this can be the whole image! Thus, it may be required to randomly pick a direction &lt;em&gt;within&lt;/em&gt; the texel area, to produce unbiased samples. I first tried to naively change the UV by random values within the texel:&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texel_coords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texture_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This works ok on large images but fails on small ones. This is due to the same problem of the density of the sphere surface being non-uniform, just now it’s within the texel as opposed to between texels. We need to take into account that latitude has the density of a cosine.&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texel_coords&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texture_size&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texel_coords&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;texel_coords&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texture_size&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;acos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ls&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.uv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using this technique, my environment important sampling is unbiased for small images, even 1x1.&lt;/p&gt;

&lt;h2 id=&quot;afterword&quot;&gt;Afterword&lt;/h2&gt;

&lt;p&gt;Environment sampling is hard! Every method has trade-offs, and practical solutions are barely good enough. This is worsened by the fact that we often need to answer the question “how likely is this sample given our env sampling routine?” for the matter of Multiple Importance Sampling (MIS). Thus, it still shows up in profiles, and will probably remain the area of research for a while. In the meantime, hopefully, these pieces of code and descriptions help you to get things sampled :)&lt;/p&gt;

&lt;p&gt;Edit: added “Equal area mapping” and “Skip self-occlusion” thanks to &lt;a href=&quot;https://github.com/ashpil&quot;&gt;@ashpil&lt;/a&gt;’s feedback.&lt;/p&gt;
</description>
        <pubDate>Sat, 19 Oct 2024 06:01:01 +0000</pubDate>
        <link>http://kvark.github.io/ray-tracing/2024/10/19/environment-sampling.html</link>
        <guid isPermaLink="true">http://kvark.github.io/ray-tracing/2024/10/19/environment-sampling.html</guid>
        
        
        <category>ray-tracing</category>
        
      </item>
    
  </channel>
</rss>
