{"@attributes":{"version":"2.0"},"channel":{"title":"The Brain Dump","description":"This is the blog and personal web page of Andre Weissflog (Floh, floooh, flohofwoe) mostly about programming stuff.\n","link":"https:\/\/floooh.github.io\/","pubDate":"Sat, 21 Feb 2026 10:58:15 +0000","lastBuildDate":"Sat, 21 Feb 2026 10:58:15 +0000","generator":"Jekyll v3.10.0","item":[{"title":"The experimental Sokol Vulkan backend","description":"<p>Update: merge happened on <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/master\/CHANGELOG.md#02-dec-2025\">02-Dec-2025<\/a>.<\/p>\n\n<p>In a couple of days I will merge the first implementation of a sokol-gfx\nVulkan backend. Please consider this backend as \u2018experimental\u2019, it has\nonly received limited testing, has limited platform coverage and some\nknown shortcomings and feature gaps which I will address in followup\nupdates.<\/p>\n\n<p>The related PRs are here:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/1350\">sokol\/#1350<\/a> - this one also\nhas all the embedded shaders for the sokol \u2018utility headers\u2019, so it looks much\nbigger than it actually is (the Vulkan backend is around the same size as\nthe GL backend, a bit over 3 kloc)<\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-tools\/pull\/196\">sokol-tools\/#196<\/a> - this\nis the update for the shader compiler which is already merged<\/li>\n<\/ul>\n\n<p>The currently known limitiations are:<\/p>\n\n<ul>\n  <li>the entire code expects a \u2018desktop GPU feature set\u2019 and doesn\u2019t implement\nfallback paths for mobile or generally ancient GPUs<\/li>\n  <li>the window system glue in sokol_app.h is only implemented for Linux\/X11 - and before the question comes up again: it works just fine on Wayland-only distros<\/li>\n  <li>only tested on an Intel Meteor Lake integrated GPU (which also means\nthat some buffer types may be allocated in memory types that are not\noptimal on GPUs without unified memory)<\/li>\n  <li>barriers for CPU =&gt; GPU updates are currently quite conservative\n(e.g. more barriers might be inserted than needed, or at a too\nearly point in a frame)<\/li>\n  <li>there\u2019s currently no GPU memory allocator, nor a way to inject\nan external GPU memory allocator like VMA (at least the latter is planned)<\/li>\n  <li>rendering is currently only supported to a single swapchain\n(not a problem when used with sokol_app.h because that also only\nsupports a single window)<\/li>\n  <li>it\u2019s currently not possible to inject native Vulkan buffers and images\ninto sokol-gfx (that\u2019s a somewhat esoteric feature supported by\nthe other backends)<\/li>\n  <li>I couldn\u2019t get RenderDoc to work, but it\u2019s unclear why<\/li>\n<\/ul>\n\n<p>On the upside:<\/p>\n\n<ul>\n  <li>no sokol-gfx API or shader-authoring changes are required\n(there are some minor breaking API changes because of some code cleanup work\nI had planned already and which are not directly related to Vulkan, but\nmost code should work without or only minimal changes)<\/li>\n  <li>the Vulkan validation layer is silent on all sokol-samples (which try to cover\nmost sokol-gfx features and their combined usage), and this includes the tricky\noptional synchronization2 validations (I\u2019m pretty proud of that considering\nthat most Vulkan samples I tried have sync-validation errors)<\/li>\n  <li>performance on my Intel Meteor Lake laptop in the <a href=\"https:\/\/floooh.github.io\/sokol-html5\/drawcallperf-sapp.html\">drawcallperf-sample<\/a>\nis already slightly better than the OpenGL backend (on a vanilla\nKubuntu system)<\/li>\n<\/ul>\n\n<p>It\u2019s also important to understand what actually motivated the Vulkan backend\n(e.g. why now, and not earlier or much later):<\/p>\n\n<p>It\u2019s <em>not<\/em> mainly about performance, but about \u2018future potential\u2019 and OpenGL\nrot. Essentially, the Vulkan backend is the first step towards deprecating the\nOpenGL backend (first, an alternative to WebGL2 had to happen - which exists now\nwith WebGPU, and next an alternative for OpenGL on Linux (and less important:\nAndroid) had to be implemented (which is the Vulkan backend). So far Linux and\nAndroid were the only sokol-gfx target platforms limited to a single backend: OpenGL.\nAll other target platforms already have a more modern alternative (Windows with\nD3D11 and macOS\/iOS with Metal). Deprecating the OpenGL backend won\u2019t happen for\na while, but personally I can\u2019t wait to free sokol-gfx from the \u2018shackles of\nOpenGL\u2019 ;)<\/p>\n\n<p>Also another reason why I felt that now is the right time to tackle Vulkan support\nis that the Vulkan API has improved quite a bit since 1.0 in ways that make it a much\nbetter fit for sokol-gfx. In a nutshell (if you already know Vulkan concepts),\nthe sokol-gfx backend makes use of the following \u2018modern\u2019 Vulkan features:<\/p>\n\n<ul>\n  <li>\u2018dynamic rendering\u2019 (e.g. render passes are enclosed by begin\/end\ncalls instead of being baked into render-pass objects) - e.g. pretty much\na copy of the Metal render pass model. This is a perfect match for\nsokol-gfx sg_begin_pass()\/sg_end_pass()<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code> - this is a controversial choice, but it\u2019s a perfect\nmatch for the sokol-gfx resource binding model and I really did not want to deal\nwith the traditional rigid Vulkan descriptor API (which is an overengineered boondoggle\nif I\u2019ve ever seen one). This is also the main reason why mobile GPUs had to be left out\nfor now, and apparently descriptor buffers are also a poor match for NVIDIA\nGPUs. The plan here is to wait until Khronos completes work on a\ndescriptor pool replacement which AFAIK will be a mix of descriptor buffers and\nD3D12-style descriptor heaps and then port the <code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code> code\nover to that new resource binding API<\/li>\n  <li>\u2018synchronization2\u2019 (not a drastic change from the original barrier model,\nI\u2019m just listing it here for completeness)<\/li>\n<\/ul>\n\n<p>Work on the Vulkan backend spans three sub-projects:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sokol-shdc<\/code>: added Vulkan-flavoured SPIRV output<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sokol_app.h<\/code>: device creation, swapchain management and frame loop<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sokol_gfx.h<\/code>: rendering and compute features<\/li>\n<\/ul>\n\n<h2 id=\"sokol-shdc-changes\">sokol-shdc changes<\/h2>\n\n<p>From the outside, the shader compiler changes are minimal (so minimal that\nthe update is actually already live for a little while).<\/p>\n\n<p>The only change is that a new output shader format has been added: <code class=\"language-plaintext highlighter-rouge\">spirv_vk<\/code>\nfor \u2018Vulkan-flavoured SPIRV.  To compile a GLSL input shader to SPIRV:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>sokol-shdc -i bla.glsl -o bla.h -l spirv_vk\n<\/code><\/pre><\/div><\/div>\n\n<p>Internally the changes are also fairly small since sokol-shdc input shaders\nare already authored in \u2018Vulkan-flavoured GLSL\u2019, the only missing information\nis the descriptor set for resource bindings.<\/p>\n\n<p>Sokol-shdc shaders only declare a bindslot on resource bindings with\ndifferent \u2018bind spaces\u2019 for uniform blocks, samplers and anything else,\nfor instance:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">fs_params<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">sampler<\/span> <span class=\"n\">smp<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Sokol-shdc performs a backend-specific bindslot allocation which for SPIRV\noutput assigns descriptor sets (uniform blocks live in descriptor set 0 and\neverything else in descriptor set 1), and remap sampler bindings to resolve\nbindslot collisions with textures, storage-buffer and storage-images, so the\nabove code snippet essentially becomes:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">set<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">fs_params<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">set<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">set<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">32<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">sampler<\/span> <span class=\"n\">smp<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The one thing that\u2019s not straightforward is that sokol-shdc does a \u2018double-tap\u2019\nfor SPIRV-output:<\/p>\n\n<ul>\n  <li>the input shader code is compiled from GLSL to SPIRV<\/li>\n  <li>SPIRVTools optimizer passes are applied to the SPIRV<\/li>\n  <li>bindings are remapped (in this case: simply add descriptor set decorators\nbut keep the bindslots intact)<\/li>\n  <li>the SPIRV is translated back to GLSL via SPIRVCross<\/li>\n  <li>finally the SPIRVCross output is compiled <em>again<\/em> to SPIRV<\/li>\n<\/ul>\n\n<p>The weird double compilation is a compromise to avoid large structural changes\nto the sokol-shdc code base and make the Vulkan shader pipeline less of a\nspecial case. Essentially, SPIRV is used as an intermediate format in the\nfirst compile pass, and then as output bytecode format in the second pass.<\/p>\n\n<h2 id=\"sokol_apph-changes\">sokol_app.h changes<\/h2>\n\n<p>Apart from the actual Vulkan-related update I took the opportunity to do some\npublic API cleanup which was rolling around in my head for a while.<\/p>\n\n<p>First, the backend-specific config options in the <code class=\"language-plaintext highlighter-rouge\">sapp_desc<\/code> struct are\nnow grouped into per-backend-nested structs, e.g. from this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sapp_desc<\/span> <span class=\"nf\">sokol_main<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span> <span class=\"n\">argc<\/span><span class=\"p\">,<\/span> <span class=\"kt\">char<\/span><span class=\"o\">*<\/span> <span class=\"n\">argv<\/span><span class=\"p\">[])<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sapp_desc<\/span><span class=\"p\">){<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">win32_console_utf8<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">win32_console_attach<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">html5_bubble_mouse_events<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">html5_use_emsc_set_main_loop<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sapp_desc<\/span> <span class=\"nf\">sokol_main<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span> <span class=\"n\">argc<\/span><span class=\"p\">,<\/span> <span class=\"kt\">char<\/span><span class=\"o\">*<\/span> <span class=\"n\">argv<\/span><span class=\"p\">[])<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sapp_desc<\/span><span class=\"p\">){<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">win32<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n          <span class=\"p\">.<\/span><span class=\"n\">console_utf8<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n          <span class=\"p\">.<\/span><span class=\"n\">console_attach<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">html5<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n          <span class=\"p\">.<\/span><span class=\"n\">bubble_mouse_events<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n          <span class=\"p\">.<\/span><span class=\"n\">use_emsc_set_main_loop<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>A new enum <code class=\"language-plaintext highlighter-rouge\">sapp_pixel_format<\/code> has been introduced which will play a bigger\nrole in the future to allow more configuration options for the sokol-app swapchain.<\/p>\n\n<p>A ton of backend-specific functions to query backend-specific objects have been\nmerged to better harmonize with sokol-gfx:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_metal_get_device<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_metal_get_current_drawable<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_metal_get_depth_stencil_texture<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_metal_get_msaa_color_texture<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_d3d11_get_device<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_d3d11_get_device_context<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_d3d11_get_render_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_d3d11_get_resolve_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_d3d11_get_depth_stencil_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_wgpu_get_device<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_wgpu_get_render_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_wgpu_get_resolve_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">sapp_wgpu_get_depth_stencil_view<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"kt\">uint32_t<\/span> <span class=\"nf\">sapp_gl_get_framebuffer<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026those have been merged into:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sapp_environment<\/span> <span class=\"nf\">sapp_get_environment<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sapp_swapchain<\/span> <span class=\"nf\">sapp_get_swapchain<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The new structs <code class=\"language-plaintext highlighter-rouge\">sapp_environment<\/code> and <code class=\"language-plaintext highlighter-rouge\">sapp_swapchain<\/code> conceptually plug into\nthe sokol-gfx structs <code class=\"language-plaintext highlighter-rouge\">sg_environment<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code> (with the emphasis on\n<strong>conceptually<\/strong>, you still need a mapping from the sokol-app structs and enums\nto the sokol-gfx structs and enums, and this mapping is still peformed by the\nsokol_glue.h header.<\/p>\n\n<p>That\u2019s it for the public API changes in sokol_app.h, now on to the Vulkan\nspecific parts:<\/p>\n\n<p>The new struct <code class=\"language-plaintext highlighter-rouge\">sapp_environment<\/code> contains a nested struct\n<code class=\"language-plaintext highlighter-rouge\">sapp_vulkan_environment vulkan;<\/code> with Vulkan object pointers (as type-erased\nvoid-pointers so that they can be tunneled through backend-agnostic code):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sapp_vulkan_environment<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">physical_device<\/span><span class=\"p\">;<\/span>  <span class=\"c1\">\/\/ VkPhysicalDevice<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">device<\/span><span class=\"p\">;<\/span>           <span class=\"c1\">\/\/ VkDevice<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">queue<\/span><span class=\"p\">;<\/span>            <span class=\"c1\">\/\/ VkQueue<\/span>\n    <span class=\"kt\">uint32_t<\/span> <span class=\"n\">queue_family_index<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sapp_vulkan_environment<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and likewise the new struct <code class=\"language-plaintext highlighter-rouge\">sapp_swapchain<\/code> contains a nested struct\n<code class=\"language-plaintext highlighter-rouge\">sapp_vulkan_swapchain vulkan;<\/code> with Vulkan object pointers which are needed\nfor a sokol-gfx swapchain render pass:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sapp_vulkan_swapchain<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">render_image<\/span><span class=\"p\">;<\/span>           <span class=\"c1\">\/\/ VkImage<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">render_view<\/span><span class=\"p\">;<\/span>            <span class=\"c1\">\/\/ VkImageView<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">resolve_image<\/span><span class=\"p\">;<\/span>          <span class=\"c1\">\/\/ VkImage;<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">resolve_view<\/span><span class=\"p\">;<\/span>           <span class=\"c1\">\/\/ VkImageView<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">depth_stencil_image<\/span><span class=\"p\">;<\/span>    <span class=\"c1\">\/\/ VkImage<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">depth_stencil_view<\/span><span class=\"p\">;<\/span>     <span class=\"c1\">\/\/ VkImageView<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">render_finished_semaphore<\/span><span class=\"p\">;<\/span>  <span class=\"c1\">\/\/ VkSemaphore<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">present_complete_semaphore<\/span><span class=\"p\">;<\/span> <span class=\"c1\">\/\/ VkSemaphore<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sapp_vulkan_swapchain<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The Vulkan-specific startup code path looks like this (the usual boilerplate-heavy\ninitialization dance):<\/p>\n\n<ul>\n  <li>A <code class=\"language-plaintext highlighter-rouge\">VkInstance<\/code> object is created.<\/li>\n  <li>A platform- and window-system-specific <code class=\"language-plaintext highlighter-rouge\">vkSurfaceKHR<\/code> object is created,\nthis is essentially the glue between a Vulkan swapchain and a specific\nwindow system. In the first release this window system glue code is only\nimplemented for X11 via <code class=\"language-plaintext highlighter-rouge\">vkCreateXlibSurfaceKHR<\/code>.<\/li>\n  <li>A <code class=\"language-plaintext highlighter-rouge\">VkPhysicalDevice<\/code> is picked, this is the first time where the sokol-app\nbackend takes a couple of shortcuts, initialization will fail if:\n    <ul>\n      <li>EXT_descriptor_buffer is not supported (this currently rules out\nmost mobile devices)<\/li>\n      <li>the supported Vulkan API version is not at least 1.3<\/li>\n      <li>no \u2018queue family\u2019 exists which supports graphics, compute, transfer\nand presentation commands all on the same queue<\/li>\n    <\/ul>\n  <\/li>\n  <li>Next a logical <code class=\"language-plaintext highlighter-rouge\">VkDevice<\/code> object is created with the following required\nfeatures and extensions (with the exception of compressed texture formats\nwhich are optional):\n    <ul>\n      <li>a single queue for all commands<\/li>\n      <li>EXT_descriptor_buffer<\/li>\n      <li>extendedDynamicState<\/li>\n      <li>bufferDeviceAddress<\/li>\n      <li>dynamicRendering<\/li>\n      <li>synchronization2<\/li>\n      <li>samplerAnisotropy<\/li>\n      <li>optional:\n        <ul>\n          <li>textureCompressionBC<\/li>\n          <li>textureCompressionETC2<\/li>\n          <li>textureCompressionASTC_LDR<\/li>\n        <\/ul>\n      <\/li>\n    <\/ul>\n  <\/li>\n  <li>The swapchain is initialized:\n    <ul>\n      <li>a <code class=\"language-plaintext highlighter-rouge\">VkSwapchainKHR<\/code> object is created:\n        <ul>\n          <li>pixel format currently either RGBA8 or BGRA8 (no sRGB)<\/li>\n          <li>present-mode hardwired to <code class=\"language-plaintext highlighter-rouge\">VK_PRESENT_MODE_FIFO_KHR<\/code><\/li>\n          <li>composite-alpha hardwired to <code class=\"language-plaintext highlighter-rouge\">VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR<\/code><\/li>\n        <\/ul>\n      <\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">VkImage<\/code> and <code class=\"language-plaintext highlighter-rouge\">VkImageView<\/code> objects are obtained or created\nfor the swapchain images, depth-stencil-buffer\nand optional MSAA surface<\/li>\n    <\/ul>\n  <\/li>\n  <li>Finally a couple of VkSemaphore objects are created for each\nswapchain image (the number of swapchain images is essentially\ndictated by the Vulkan driver):\n    <ul>\n      <li>one <code class=\"language-plaintext highlighter-rouge\">render_finished_semaphore<\/code> which signals that the GPU\nhas finished rendering to a swapchain surface<\/li>\n      <li>one <code class=\"language-plaintext highlighter-rouge\">present_complete_semaphore<\/code> which signals that presenting\na swapchain image has completed and the image ready for reuse<\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>At this point, the Vulkan specific code in sokol_app.h is at about 600\nlines of code, which is a lot of boilerplate, but OTH is a lot less messy\nthan the combined OpenGL window system code for GLX, EGL, WGL or NSOpenGL\n(yet still a lot more than the window system glue for the other backends).<\/p>\n\n<p>The actually interesting stuff happens in the last two Vulkan backend functions:<\/p>\n\n<p>The internal function <code class=\"language-plaintext highlighter-rouge\">_sapp_vk_swapchain_next()<\/code> is a wrapper around\n<code class=\"language-plaintext highlighter-rouge\">vkAcquireNextImageKHR()<\/code> and obtains the next free swapchain image. The\nfunction will also signal the associated <code class=\"language-plaintext highlighter-rouge\">present_complete_semaphore<\/code>.<\/p>\n\n<p>The last function in the sokol-app Vulkan backend is <code class=\"language-plaintext highlighter-rouge\">_sapp_vk_present()<\/code>, this\nis a wrapper for <code class=\"language-plaintext highlighter-rouge\">vkQueuePresentKHR()<\/code>. The present operation uses the\n<code class=\"language-plaintext highlighter-rouge\">render_finished_semaphore<\/code> to make sure that presentation happens after the GPU\nhas finished rendering to the swapchain image. When the <code class=\"language-plaintext highlighter-rouge\">vkQueuePresentKHR()<\/code>\nfunction returns with <code class=\"language-plaintext highlighter-rouge\">VK_ERROR_OUT_OF_DATE_KHR<\/code> or <code class=\"language-plaintext highlighter-rouge\">VK_SUBOPTIMAL_KHR<\/code>, the\nswapchain resources are recreated (this happens for instance when the window is\nresized).<\/p>\n\n<p>There\u2019s a couple of open todo points in the sokol-app Vulkan backend which\nI\u2019ll take care of later:<\/p>\n\n<ul>\n  <li>Any non-success return values from <code class=\"language-plaintext highlighter-rouge\">vkAcquireNextImageKHR()<\/code> are currently\nonly logged but not handled. Normally the application is either supposed\nto re-create the swapchain resources or skip rendering and presentation.\nSince I couldn\u2019t coerce my Kubuntu laptop to ever return a non-success value\nfrom <code class=\"language-plaintext highlighter-rouge\">vkAcquireNextImageKHR()<\/code> I would have to implement behaviour I couldn\u2019t\ntest, so I had to skip this part for now. Maybe when moving the code over\nto my Windows\/NVIDIA PC I\u2019ll be able to handle that situation properly.<\/li>\n  <li>Currently the swapchain image size must match the window client rectangle\nsize (same as OpenGL via GLX). The Vulkan swapchain API has an\noptional scaling feature, but I couldn\u2019t get this to work on my Kubuntu\nlaptop. Window-system scaling is mainly useful when the system has a\nhigh-dpi display but lower-end GPU, and all other sokol-app backends\ndepend on the system to scale a smaller framebuffer to the window client\nrectangle when needed.<\/li>\n<\/ul>\n\n<p>The main area I struggled with in the sokol-app Vulkan backend was\nswapchain resizing. Most sokol-app backends kick off any swapchain\nresize operation from the window system\u2019s resize event, e.g.:<\/p>\n\n<ul>\n  <li>window is resized by user<\/li>\n  <li>window system resize event fires giving the new window size<\/li>\n  <li>sokol-app listens for the window system resize event and initiates\na swapchain resize with the new size coming from the window system\nevent, then stores the new size for sapp_width\/height() and finally\nfires an <code class=\"language-plaintext highlighter-rouge\">SAPP_EVENTTYPE_RESIZED<\/code> event<\/li>\n<\/ul>\n\n<p>This doesn\u2019t work on the Vulkan backend, the validation layer would sometimes\ncomplain that there\u2019s a difference between actual and expected swapchain\nsurface dimensions (I forgot the exact error circumstances, forgiveable since\nimplementating a Vulkan backend is basically crawling from one validation\nlayer error to the next).<\/p>\n\n<p>Long story short: I got it to work by leaving the host window system entirely\nout of the loop and let the Vulkan swapchain take full control of the resize\nprocess:<\/p>\n\n<ul>\n  <li>window is resized by user<\/li>\n  <li>window system resize event fires, but is now ignored by sokol-app<\/li>\n  <li>the next time <code class=\"language-plaintext highlighter-rouge\">vkQueuePresentKHR()<\/code> is called it returns with an error code\nand this triggers a swapchain-resource resize, with the size coming from\nthe Vulkan surface object instead of the window system, finally an\n<code class=\"language-plaintext highlighter-rouge\">SAPP_EVENTTYPE_RESIZED<\/code> event is fired<\/li>\n<\/ul>\n\n<p>This fixes any validation layer warnings and is in the end a cleaner\nimplementation compared to letting the window system dictate the swapchain size.<\/p>\n\n<p>There are downsides though: At least on my Kubuntu laptop it looks like the\nwindow system and Vulkan swapchain code doesn\u2019t run in lock step. Instead the\nVulkan swapchain seems to lag behind the window system a bit and this results in\nminor artefacts during resizing: sometimes there\u2019s a visible gap between the\nVulkan surface and window border, and the frame rate gets slighly out of whack\nduring resize. In comparison, on macOS rendering with Metal during window resize\nis buttery smooth and without resize-jitter or border-gaps (although tbf,\nremoving the resize-jitter on macOS had to be explicitly implemented by\nanchoring the NSView object to a window border).<\/p>\n\n<p>That\u2019s all there is to the Vulkan backend in sokol_app.h, on to sokol_gfx.h!<\/p>\n\n<h2 id=\"sokol_gfxh-changes\">sokol_gfx.h changes<\/h2>\n\n<p>For the most part, the actual mapping of the sokol-gfx functions to Vulkan API\nfunctions is very straightforward, often the mapping is 1:1. This is\nmainly thanks to using a couple of modern Vulkan features and extensions:<\/p>\n\n<ul>\n  <li>Dynamic rendering (e.g. <code class=\"language-plaintext highlighter-rouge\">vkBeginRendering()\/vkEndRendering()<\/code>) is a perfect match\nfor sokol-gfx <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()\/sg_end_pass()<\/code>, this is not very surprising though\nbecause the dynamic rendering Vulkan API is basically a \u2018de-OOP-ed\u2019 version of the Metal\nrender pass API.<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffers<\/code> is an absolutely perfect match for sokol-gfx\u2019s\n<code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> call, and a \u2018pretty good\u2019 match for <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code><\/li>\n<\/ul>\n\n<p>The main areas for future improvements are the barrier system and the staging\nsystem, but let\u2019s not get ahead of ourselves.<\/p>\n\n<h3 id=\"a-10000-foot-view\">A 10000 foot view<\/h3>\n\n<p>Apart from the straight mapping of sokol-gfx API calls to Vulkan-API calls, the\nVulkan backend has to implement a couple of low-level subsystems. This isn\u2019t\nall that unusual, other backends also have such subsystems, but the Vulkan\nbackend definitely is the most \u2018subsystem heavy\u2019.<\/p>\n\n<p>OTH some concepts of modern Vulkan are quite similar to WebGPU, Metal and even\nD3D11 - and this conceptual overlap significantly simplified the Vulkan\nbackend implementation.<\/p>\n\n<p>In some areas the Vulkan backend has even more straightforward implementations than\nsome of the other backends, for instance the implementation of the resource binding\ncall <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings<\/code> in the Vulkan backend is one of the most straightforward\nof all backends and especially compared to the WebGPU backend. In Vulkan it\u2019s\nliterally just a bunch of memcpy\u2019s followed by a single Vulkan API call to\nrecord an offset into the descriptor buffer (ok, it\u2019s actually a bit more\ncomplicated because of the barrier system). Compared to that, the WebGPU backend\nneeds to use a \u2018hash-and-cache\u2019 approach for baked BindGroup objects, e.g.\ncalling <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> may involve creating and destroying WebGPU objects.<\/p>\n\n<p>The low-level subsystems in the sokol-gfx Vulkan backend are:<\/p>\n\n<ul>\n  <li>a \u2018delete queue\u2019 system for delayed Vulkan object destruction<\/li>\n  <li>the GPU memory allocation system (very rudimentary at the moment)<\/li>\n  <li>the frame-sync system (e.g. ensuring that the CPU and GPU can work\nin parallel in typical render frames)<\/li>\n  <li>the uniform update system<\/li>\n  <li>the bindings update system<\/li>\n  <li>two \u2018staging systems\u2019 for copying CPU-side data into GPU-side resources:\n    <ul>\n      <li>a \u2018copy\u2019 staging system<\/li>\n      <li>a \u2018stream\u2019 staging system<\/li>\n    <\/ul>\n  <\/li>\n  <li>the resource barrier system<\/li>\n<\/ul>\n\n<p>Let\u2019s look at those one by one:<\/p>\n\n<h3 id=\"the-delete-queue-system\">The Delete Queue System<\/h3>\n\n<p>Vulkan doesn\u2019t have any automatic lifetime management like some other 3D APIs\n(e.g. no D3D-style reference counting). When you call a destroy function on\nan object, it\u2019s gone. When you do that while the object is still in flight\n(e.g. referenced in a queue and waiting to be consumed by the GPU), hilarity\nensues.<\/p>\n\n<p>IMHO this is much better than any automatic lifetime management system, because\nit avoids any confusion about reference counts (e.g. questions like: when I call\nthis function to get an object reference, will that bump the refcount or not?),\nbut this means that a Vulkan backend needs to implement some sort of garbage\ncollection on its own.<\/p>\n\n<p>Sokol-gfx uses a double-buffered delete-queue system for this. Each\n\u2018double-buffer-frame-context\u2019 owns a delete queue which is a simple fixed-size\narray of pointer-pairs. Each queue item consists of:<\/p>\n\n<ul>\n  <li>one type-erased Vulkan object pointer (e.g. a void-pointer)<\/li>\n  <li>a function pointer for a destructor function which takes a\nvoid* as argument and knows how to destroy that Vulkan object<\/li>\n<\/ul>\n\n<p>All Vulkan object types which may be referenced in command buffers will not\ncall their <code class=\"language-plaintext highlighter-rouge\">vkDestroy*()<\/code> functions directly, but instead add them to the\ndelete-queue that\u2019s associated with the currently recorded command buffer. At\nthe start of a new frame (what \u2018new frame\u2019 actually means is explained down in\nthe \u2018frame-sync system\u2019), the delete-queue for that frame-context is drained by\ncalling the destructor function with the Vulkan object pointer of a queue item.\nThis makes sure that any Vulkan objects are kept alive until the GPU has finished\nprocessing any command buffers which might hold references to those objects.<\/p>\n\n<h3 id=\"the-gpu-memory-allocation-system\">The GPU Memory Allocation System<\/h3>\n\n<p>Currently GPU allocations do <em>not<\/em> go through a custom allocator, instead\nall granular allocations directly call into <code class=\"language-plaintext highlighter-rouge\">vkAllocateMemory()<\/code>. Originally\nI had intended to use SebAaltonen\u2019s <a href=\"https:\/\/github.com\/sebbbi\/OffsetAllocator\">OffsetAllocator<\/a>\nas the default GPU allocator, but also expose an allocator interface to allow\nusers to inject more complex allocators like <a href=\"https:\/\/github.com\/GPUOpen-LibrariesAndSDKs\/VulkanMemoryAllocator\">VMA<\/a>.<\/p>\n\n<p>Historically a custom allocator was pretty much required because some Vulkan\ndrivers only allowed 4096 unique GPU allocations. Today though it looks\nlike pretty much all (desktop) Vulkan drivers allow 4 billion allocations (at least\naccording to the <a href=\"https:\/\/vulkan.gpuinfo.org\/\">Vulkan hardware database<\/a>).<\/p>\n\n<p>The plan is still to at least allow injecting a custom GPU allocator via an\nallocator interface, and also maybe to integrate OffsetAllocator as default\nallocator, but without knowing the memory allocation strategy of Vulkan drivers\nthis may be redundant. E.g. if a Vulkan driver essentially integrates something\nlike VMA anyway there\u2019s not much point stacking another allocator on top of it,\nat least for a fairly high level API wrapper like sokol-gfx.<\/p>\n\n<p>In any case, the current GPU memory allocation implementation is prepared for\na bit more abstraction in the future. All GPU allocations go through a single\ninternal function <code class=\"language-plaintext highlighter-rouge\">_sg_vk_mem_alloc_device_memory()<\/code> which takes a \u2018memory type\u2019\nenum and a <code class=\"language-plaintext highlighter-rouge\">VkMemoryRequirements<\/code> pointer as input. The memory type enum is\nsokol-gfx specific and includes:<\/p>\n\n<ul>\n  <li>storage buffer (an sg_buffer object with storage buffer usage)<\/li>\n  <li>generic buffer (all other sg_buffer types)<\/li>\n  <li>image (all usages)<\/li>\n  <li>internal staging buffer for the \u2018copy-staging system\u2019<\/li>\n  <li>internal staging buffer for the \u2018stream-staging system\u2019<\/li>\n  <li>internal uniform buffer<\/li>\n  <li>internal descriptor buffer<\/li>\n<\/ul>\n\n<p>Currently all resources are either in \u2018device-local\u2019 memory, or in\n\u2018host-visible + host-coherent\u2019 memory. Having the mapping from sokol-specific\nmemory type to Vulkan memory flags in one place makes it easier to tweak those\nflags in the future (or delegate that decision to an external memory allocator).<\/p>\n\n<h3 id=\"the-frame-sync-system\">The Frame Sync System<\/h3>\n\n<p>The frame sync system is mainly concerned about letting the CPU and GPU work\nin parallel without stepping on each other\u2019s feet. This basically comes down\nto double-buffering all resources which are written by the CPU and read by the\nGPU, and to have one sync-point in a sokol-gfx frame where the CPU needs to\nwait for the oldest \u2018frame-context\u2019 to become available (e.g. is no longer\n\u2018in flight\u2019).<\/p>\n\n<p>This single <code class=\"language-plaintext highlighter-rouge\">CPU &lt;=&gt; GPU<\/code> sync point is implemented in a function\n<code class=\"language-plaintext highlighter-rouge\">_sg_vk_acquire_frame_command_buffers()<\/code>.  The name indicates the main feature\nof that function: it acquires command buffers to record the Vulkan commands of\nthe current frame. Command buffers are reused, so this involves waiting for the\ncommand buffers to become available (e.g. they are no longer read from by the\nGPU). \u201cCommand buffers\u201d is plural because there are two command buffers per frame:\none which records all staging-commands, and one for the actual compute\/render\ncommands - more on that later in the staging system section.<\/p>\n\n<p>For this <code class=\"language-plaintext highlighter-rouge\">CPU &lt;=&gt; GPU<\/code> synchronization, each double-buffered frame-context owns\na <code class=\"language-plaintext highlighter-rouge\">VkFence<\/code> which is signalled when the GPU is done processing a \u2018queue submit\u2019.<\/p>\n\n<p>So the first and most important thing the <code class=\"language-plaintext highlighter-rouge\">_sg_vk_acquire_frame_command_buffers()<\/code> function\ndoes is to wait for the fence of the oldest frame-context with a call to <code class=\"language-plaintext highlighter-rouge\">vkWaitForFences()<\/code>.<\/p>\n\n<p>This potential-wait-operation is the reason why sokol-gfx applications should move\nsokol-gfx calls towards the end of the frame callback and try to do all\nheavy non-rendering-related CPU work at the start of the frame callback.\nMore specifically calls to:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_update_buffer()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_update_image()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_append_buffer()<\/code><\/li>\n<\/ul>\n\n<p>\u2026these are basically the \u2018potential new-frame entry points\u2019 of the sokol-gfx\nAPI which may require the CPU to wait for the GPU.<\/p>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">_sg_vk_acquire_frame_command_buffers()<\/code> function does a couple more things\nafter <code class=\"language-plaintext highlighter-rouge\">vkWaitForFences()<\/code> returns:<\/p>\n\n<ul>\n  <li>first (actually before the <code class=\"language-plaintext highlighter-rouge\">vkWaitForFences()<\/code> call) it checks if the function\nhad already been called in the current frame, if yes it returns immediately<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">vkResetFences()<\/code> is called on the fence we just waited on<\/li>\n  <li>the delete-queue is drained (e.g. all resources which were recorded for\ndestruction in the frame-context we just waited on are finally destroyed)<\/li>\n  <li>any command buffers associated with the new frame are reset via <code class=\"language-plaintext highlighter-rouge\">vkResetCommandBuffer()<\/code><\/li>\n  <li>\u2026and recording into those command buffers is started via <code class=\"language-plaintext highlighter-rouge\">vkBeginCommandBuffer()<\/code><\/li>\n  <li>additionally the other subsystems are notified because they might want to do their\nown thing:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_uniform_after_acquire()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_bind_after_acquire()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_staging_stream_after_acquire()<\/code><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>The other internal function of the frame-sync system is <code class=\"language-plaintext highlighter-rouge\">_sg_vk_submit_frame_command_buffers()<\/code>.\nThis is called at the end of a \u2018sokol-gfx frame\u2019 in the <code class=\"language-plaintext highlighter-rouge\">sg_commit()<\/code> call. The main job\nof this function is to submit the recorded command buffers for the current frame\nvia <code class=\"language-plaintext highlighter-rouge\">vkQueueSubmit()<\/code>. This submit operation uses the two semaphores we got handed\nfrom the outside world (e.g. sokol-app) as part of the swapchain information\nin <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code>:<\/p>\n\n<ul>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">present_complete_semaphore<\/code> is used as the wait-semaphore of the\n<code class=\"language-plaintext highlighter-rouge\">vkQueueSubmit()<\/code> call (the GPU basically needs to wait for the swapchain image\nof the render pass to become available for reuse)<\/li>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">render_finished_semaphore<\/code> is used as the signal-semaphore to be signalled\nwhen the GPU is done processing the submit payload<\/li>\n<\/ul>\n\n<p>Before the <code class=\"language-plaintext highlighter-rouge\">vkQueueSubmit()<\/code> call there\u2019s a bit more housekeeping happening:<\/p>\n\n<ul>\n  <li>the other subsystems are notified about the submit via:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_staging_stream_before_submit()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_bind_before_submit()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">_sg_vk_uniform_before_submit()<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>recording into the command buffers which are associated with the current\nframe context is finished via <code class=\"language-plaintext highlighter-rouge\">vkEndCommandBuffers()<\/code><\/li>\n<\/ul>\n\n<p>It\u2019s also important to note that there is one other potential <code class=\"language-plaintext highlighter-rouge\">CPU &lt;=&gt; GPU<\/code>\nsync-point in a frame, and that\u2019s in the first <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> for a\nswapchain render pass: the swapchain-info struct that\u2019s passed into\n<code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> contains a swapchain image which must be acquired via\n<code class=\"language-plaintext highlighter-rouge\">vkAcquireNextImageKHR()<\/code> (when using sokol_app.h this happens in the\n<code class=\"language-plaintext highlighter-rouge\">sapp_get_swapchain()<\/code> call - usually indirectly via <code class=\"language-plaintext highlighter-rouge\">sglue_swapchain()<\/code>).<\/p>\n\n<p>That is all for the frame-sync system in sokol-gfx, all in all quite similar to\nMetal or WebGPU, just with more code bloat (as is the Vulkan way).<\/p>\n\n<h3 id=\"resource-binding-via-ext_descriptor_buffer\">Resource binding via EXT_descriptor_buffer<\/h3>\n\n<p>\u2026a little detour into Vulkan descriptors and how the sokol-gfx resource binding\nmodel maps to Vulkan.<\/p>\n\n<p>Conceptually and somewhat simplified, a Vulkan <strong>descriptor<\/strong> is an abstract\nreference to a Vulkan buffer, image or sampler which needs to be accessible in a\nshader. Basically what shows up on the shader side whenever you see a\n<code class=\"language-plaintext highlighter-rouge\">layout(binding=x) ...<\/code>. In sokol-gfx lingo this is called a \u2018binding\u2019.<\/p>\n\n<p>In an ideal world, such a binding would simply be a \u2018GPU pointer\u2019 to some\nopaque struct living in GPU memory which describes to shader code how\nto access bytes in a storage buffer, pixels in a storage image, or how\nto perform a texture-sampling operation.<\/p>\n\n<p>In the real world it\u2019s not that simple because this is exactly the one main area\nwhere GPU architectures still differ dramatically: on some GPUs this information might be\nhardwired into register tables and\/or involves fixed-function features instead\nof being just \u2018structs in GPU memory\u2019 - and unfortunately those differences are\nnot limited to shitty mobile GPUs, but are also still present in desktop GPUs.\nIntel, AMD and NVIDIA all have different opinions on how this whole resource binding\nthing should work - and I\u2019m not sure anything has changed in the last decade\nsince Vulkan promised us a more-or-less direct mapping to the underlying hardware.<\/p>\n\n<p>So in the real world 3D APIs still need to come up with some sort of abstraction\nlayer to get all those different hardware resource binding models under a common\nprogramming model (and yes, even the apparently \u2018low-level\u2019 Vulkan API had to\ncome up with a highlevel abstraction for resource binding - and this went quite\npoorly\u2026 but I disgress).<\/p>\n\n<p>(side note: traditional vertex- and index-buffer-bindings are <em>not<\/em> performed\nthrough Vulkan descriptors, but through regular \u2018bindslot-setter\u2019 calls like in\nany other 3D API - go figure).<\/p>\n\n<p>A Vulkan <strong>descriptor-set<\/strong> is a group of such concrete bindings which can be\napplied as an atomic unit instead of applying each binding individually. In\nthe end the traditional Vulkan descriptor model isn\u2019t all that different from\nthe \u2018old\u2019 bindslot model used in Metal V1 or D3D11, the one big and important\ndifference is that bindings are not applied individually but as groups.<\/p>\n\n<p>The downside of such a \u2018bind group model\u2019 is of course that specific binding\ncombinations may be unpredictable - which is the one big recurring topic in\nVulkan\u2019s (very slow) API evolution.<\/p>\n\n<p>In \u2018old Vulkan\u2019 pretty much all state-combinations in all areas of the API need\nto be known upfront in order to move as much work as possible into the\ninit-phase and out of the render-phase. Theoretically a pretty sensible plan,\nbut unfortunately only theoretically. In practice there are a lot of use cases\nwhere pre-baking everything is simply not possible, especially outside the game\nengine world, and even in gaming it doesn\u2019t quite work - whenever you see\nstuttering when something new appears on screen in modern games built on top of\nstate-of-the-art engines calling into modern 3D APIs - that\u2019s most likely the core design\nphilosophy of Vulkan and D3D12 crashing and burning after colliding with\nreality. Thankfully - but unfortunately very slowly - this is changing. Most of Vulkan\u2019s\nprogress in the last decade was about rolling the core API back to a more \u2018dynamic\u2019\nprogramming model.<\/p>\n\n<p>Ok, back to Vulkan\u2019s resource binding lingo:<\/p>\n\n<p>A Vulkan <strong>descriptor-set-layout<\/strong> is the <em>shape<\/em> of a descriptor-set.\nIt basically says \u2018there will be a sampled texture at binding 0, a buffer at\nbinding 1 and a sampler at binding 2\u2019, but not the concrete texture, buffer or\nsampler objects (those are referenced in the concrete <strong>descriptor-sets<\/strong>).<\/p>\n\n<p>And finally a Vulkan <strong>pipeline-layout<\/strong> groups all descriptor-set-layouts required\nby the shader stages of a Vulkan pipeline-state-object.<\/p>\n\n<p>When coming from WebGPU this should all sound quite familiar since the\nWebGPU bindgroups model is essentially the Vulkan 1.0 descriptor model\n(for better or worse):<\/p>\n\n<ul>\n  <li>WebGPU BindGroupEntry maps to Vulkan descriptors<\/li>\n  <li>WebGPU BindGroup maps to Vulkan descriptor sets<\/li>\n  <li>WebGPU BindGroupLayout maps to Vulkan descriptor set layouts<\/li>\n  <li>WebGPU PipelineLayout maps to Vulkan pipeline layouts<\/li>\n<\/ul>\n\n<p>\u2018Old Vulkan\u2019 then adds descriptor pools on top of that but tbh I didn\u2019t\neven bother to deal with those and skipped right to <code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code>.<\/p>\n\n<p>With the descriptor buffer extension, descriptors and descriptor sets are \u2018just\nmemory\u2019 with opaque memory layouts for each descriptor type which are\nspecific to the Vulkan driver (depending on the driver and descriptor type, such\nopaque memory blobs seem to be between 16 and 256 bytes per descriptor).<\/p>\n\n<p>Binding resources with <code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffers<\/code> essentially looks like this:<\/p>\n\n<p>In the init-phase:<\/p>\n<ul>\n  <li>create a descriptor buffer big enough to hold all descriptors needed in\na worst-case frame<\/li>\n  <li>for each item in a descriptor-set-layout, ask Vulkan for the descriptor size and\nrelative offset to the start of the descriptor-set data in the descriptor buffer<\/li>\n  <li>similar for all concrete descriptors, ask Vulkan to copy their opaque memory\nrepresentation into some private memory location and keep those around for the render\nphase (of course it\u2019s also possible to move this step into the render phase)<\/li>\n<\/ul>\n\n<p>In the render-phase:<\/p>\n<ul>\n  <li>memcpy the concrete descriptor blobs we stored upfront into the descriptor\nbuffer to compose an adhoc descriptor set, using the offsets we also stored upfront<\/li>\n  <li>finally record the start offset in the descriptor buffer into a Vulkan command\nbuffer via a Vulkan API call, and that\u2019s it!<\/li>\n<\/ul>\n\n<p>This is pretty much the same procedure how uniform data updates are\nperformed in the sokol-gfx Metal and WebGPU backends, now just extended to\nresource bindings.<\/p>\n\n<p>E.g. TL;DR: both uniform data snippets and resource bindings are\n\u2018just frame-transient data snippets\u2019 which are memcpy\u2019ed into per-frame\nbuffers and the buffer offsets recorded before the next draw- or dispatch-call.<\/p>\n\n<p>In sokol-gfx, the VkDescriptorSetLayout and VkPipelineLayout objects are created\nin <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> using the shader interface reflection information provided\nin the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> arg (which is usually code-generated by the sokol-shdc\nshader compiler).<\/p>\n\n<ul>\n  <li>the first descriptor set layout (set 0) describes all uniform block bindings\nused by the shader across all shader stages<\/li>\n  <li>the second descriptor set layout (set 1) describes all texture, storage buffer,\nstorage image and sampler bindings<\/li>\n<\/ul>\n\n<p>\u2026additionally, <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> queries the descriptor sizes and offsets\nwithin their descriptor set.<\/p>\n\n<h3 id=\"the-uniform-update-system\">The uniform update system:<\/h3>\n\n<p>Conceptually uniform updates in the Vulkan backend are similar to the Metal backend:<\/p>\n\n<ul>\n  <li>a double-buffered uniform buffer big enough to hold all uniform updates\nfor a worst-case frame, allocated in host-visible memory (so that the memory\nis directly writable by the CPU and directly readable by the GPU)<\/li>\n  <li>a call to <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> memcpy\u2019s the uniform data snippet into the\nnext free uniform buffer location (taking alignment requirements into account),\nthis happens individually for the up to 8 \u2018uniform block slots\u2019<\/li>\n  <li>before the next draw- or dispatch-call, the offsets into the uniform buffer for the up to\n8 uniform block slots are recorded into the current command buffer<\/li>\n<\/ul>\n\n<p>The last step of recording the uniform-buffer offsets is delayed into the next\ndraw- or dispatch-call to avoid redundant work. This is because <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code>\nworks on a single uniform block slot, but in Vulkan all uniform block slots are\ngrouped into one descriptor set, and we only want to apply that descriptor-set\nat most once per draw\/dispatch call.<\/p>\n\n<p>The actual <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> call is extremely cheap since no Vulkan API\ncalls are performed:<\/p>\n\n<ul>\n  <li>a simple memcpy of the uniform data snippet into the per-frame uniform buffer<\/li>\n  <li>writing the \u2018GPU buffer address\u2019 and snippet size into a cached\narray of <code class=\"language-plaintext highlighter-rouge\">VkDescriptorAddressInfoEXT<\/code> structs<\/li>\n  <li>setting a \u2018uniforms dirty flag\u2019.<\/li>\n<\/ul>\n\n<p>\u2026then later in the next draw- or dispatch-calls if the \u2018uniforms dirty flag\u2019\nis set the actual uniform block descriptor set binding happens:<\/p>\n\n<ul>\n  <li>for each uniform block used in the current pipeline\/shader, a opaque descriptor\nmemory blob is directly written into the frame\u2019s descriptor buffer via\na call to <code class=\"language-plaintext highlighter-rouge\">vkGetDescriptorEXT()<\/code><\/li>\n  <li>the start offset of the descriptor-set in the descriptor buffer is recorded\ninto the current frame command buffer via <code class=\"language-plaintext highlighter-rouge\">vkCmdSetDescriptorBufferOffsetsEXT()<\/code><\/li>\n<\/ul>\n\n<p>\u2026delaying the operation to record the uniform buffer offsets into the draw- or\ndispatch-call to avoid redundant API calls is actually something that I will also\nneed to implement in the WebGPU backend (I was taking notes while implementing\nthe Vulkan backend which improvements could be back-ported to the WebGPU\nbackend, and I\u2019ll take care of those right after the Vulkan backend is merged).<\/p>\n\n<h3 id=\"the-resource-binding-system\">The resource binding system<\/h3>\n\n<p>Updating resource bindings via <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> is very similar to the\nuniform update system, but actually even simpler because no extra uniform buffer\nis involved, and some more initialization can be moved into the init-phase when\ncreating view objects:<\/p>\n\n<p>When creating a texture-, storage-buffer- or storage-image-view object via\n<code class=\"language-plaintext highlighter-rouge\">sg_make_view()<\/code> or a sampler object via <code class=\"language-plaintext highlighter-rouge\">sg_make_sampler)<\/code>, the concrete\ndescriptor data (those little 16..256 byte opaque memory blobs) is copied into\nthe sokol-gfx view or sampler object via <code class=\"language-plaintext highlighter-rouge\">vkGetDescriptorEXT()<\/code>.<\/p>\n\n<p>Then <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> is just a couple of memcpy\u2019s and a Vulkan call:<\/p>\n\n<ul>\n  <li>for each view and sampler in the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> argument, a memcpy of the\ndescriptor memory blob which was stored in the sokol-gfx object\ninto the current frame\u2019s descriptor buffer happens - e.g. no\nVulkan calls for that\u2026<\/li>\n  <li>finally a single call to <code class=\"language-plaintext highlighter-rouge\">vkCmdSetDescriptorBufferOffsetsEXT()<\/code> records\nthe descriptor buffer offset into the current frame\u2019s command buffer<\/li>\n<\/ul>\n\n<p>Vertex- and index-buffer bindings happen via traditional bindslot calls\n(<code class=\"language-plaintext highlighter-rouge\">vkCmdBindVertexBuffers<\/code> and <code class=\"language-plaintext highlighter-rouge\">vkCmdBindIndexBuffer<\/code>). Additionally,\nbarriers may be inserted inside <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> but that will be explained\nfurther down in the barrier system.<\/p>\n\n<h3 id=\"the-two-staging-systems\">The two staging systems<\/h3>\n\n<p>Sokol-gfx currently has two separate staging systems for uploading CPU-side\ndata into GPU-memory with the rather arbitrary names \u2018copy-staging-system\u2019 and\n\u2018stream-staging-system\u2019. Both can upload data into buffers and images, but with\ndifferent compromises:<\/p>\n\n<ul>\n  <li>the \u2018copy-staging-system\u2019 can upload large amounts of data through a single\nsmall staging buffer (default size: 4 MB), with the downside that the Vulkan\nqueue needs to be flushed (e.g. a <code class=\"language-plaintext highlighter-rouge\">vkQueueWaitIdle()<\/code> is involved)<\/li>\n  <li>the \u2018stream-staging-system\u2019 can upload a limited amount of data per-frame\nthrough a fixed-size double-buffered staging buffer (default size: 16 MB -\nbut this can be tweaked in the <code class=\"language-plaintext highlighter-rouge\">sg_setup()<\/code> call of course), this doesn\u2019t cause\nany frame-pacing \u2018disruptions\u2019 like the copy-staging-system does<\/li>\n<\/ul>\n\n<p>The copy-staging-system is currently used:<\/p>\n\n<ol>\n  <li>to upload initial content into immutable buffers and images within\n<code class=\"language-plaintext highlighter-rouge\">sg_make_buffer()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_make_image()<\/code><\/li>\n  <li>to upload data into <code class=\"language-plaintext highlighter-rouge\">usage.dynamic_update<\/code> images and buffers\nin the <code class=\"language-plaintext highlighter-rouge\">sg_update_buffer()<\/code>, <code class=\"language-plaintext highlighter-rouge\">sg_append_buffer()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_update_image()<\/code> calls<\/li>\n<\/ol>\n\n<p>The stream-staging system is only used for <code class=\"language-plaintext highlighter-rouge\">usage.stream_update<\/code> resources\nwhen calling <code class=\"language-plaintext highlighter-rouge\">sg_update_buffer()<\/code>, <code class=\"language-plaintext highlighter-rouge\">sg_append_buffer()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_update_image()<\/code>.<\/p>\n\n<p>This means that the correct choice of <code class=\"language-plaintext highlighter-rouge\">usage.dynamic_update<\/code> and\n<code class=\"language-plaintext highlighter-rouge\">usage.stream_update<\/code> for buffers and images is much more important in the\nVulkan backend than in other backends.<\/p>\n\n<p>In general:<\/p>\n\n<ul>\n  <li>creating an immutable buffer or image <strong>with initial content<\/strong> in the\nrender-phase will \u2018disrupt\u2019 rendering (how bad this disruption actually is\nremains to be seen though)<\/li>\n  <li>the same disruption happens for updating a buffer or image with <code class=\"language-plaintext highlighter-rouge\">usage.dynamic_update<\/code>,<\/li>\n  <li>make sure to use <code class=\"language-plaintext highlighter-rouge\">usage.stream_update<\/code> for buffers and images that need to be updated each\nframe, but be aware that those uploads go through a single per-frame staging\nbuffer which needs to be big enough to hold all stream-uploads in a single\nframe (staging buffer sizes can be adjusted in the sg_setup() call)<\/li>\n<\/ul>\n\n<p>The strategy for updating <code class=\"language-plaintext highlighter-rouge\">usage.dynamic_update<\/code> resources may change in the future. For\ninstance I was considering treating dynamic-updates exactly the same as\nstream-updates (e.g. going through the per-frame staging buffer to avoid\nthe <code class=\"language-plaintext highlighter-rouge\">vkQueueWaitIdle()<\/code>), and when the staging buffer would overflow\nfall back to the copy-staging system (also for stream-updates). This\nfelt too unpredictable to me, so I didn\u2019t go that way for now.<\/p>\n\n<p>Note that the staging system is the most likely system to drastically change\nin the future (together with the barrier system). One of the important planned\nchanges in my mental sokol-gfx roadmap is a rewrite of the resource update API,\nand this rewrite will most likely \u2018favour\u2019 modern 3D APIs and not worry about\nOpenGL as much as the current very restrictive resource update API does.<\/p>\n\n<p>The common part in both staging systems is how the actual upload happens:<\/p>\n\n<ul>\n  <li>staging buffers are allocated in CPU-visible + cache-coherent memory\n(the copy-staging system uses a single small buffer, while the stream-staging\nsystem uses double-buffering)<\/li>\n  <li>a staging operation first memcpy\u2019s a chunk of memory into the staging\nbuffer and then records a Vulkan command to copy that data from the\nstaging buffer into a Vulkan buffer or image (via <code class=\"language-plaintext highlighter-rouge\">vkCmdCopyBuffer()<\/code> or\n<code class=\"language-plaintext highlighter-rouge\">vkCmdCopyBufferToImage2()<\/code><\/li>\n  <li>in the stream-staging system each buffer update is always a single call\nto <code class=\"language-plaintext highlighter-rouge\">vkCmdCopyBuffer()<\/code> and each image update is always one call to\n<code class=\"language-plaintext highlighter-rouge\">vkCmdCopyBufferToImage2()<\/code> per mipmap<\/li>\n  <li>in the copy-staging-system, staging operations which are bigger than the\nstaging buffer size will be split into multiple copy operations,\neach copy-step involving a <code class=\"language-plaintext highlighter-rouge\">vkQueueWaitIdle<\/code><\/li>\n  <li>overflowing the stream-staging buffer is a \u2018soft error\u2019, e.g. an\nerror will be logged but otherwise this is a no-op<\/li>\n<\/ul>\n\n<p>There is another notable implementation detail in the stream-staging\nsystem which is related to the barrier system:<\/p>\n\n<p>All stream-staging copy commands are recorded into a separate Vulkan command\nbuffer object so that they are not interleaved with the compute\/render commands\nwhich are recorded into the regular per-frame command buffer.<\/p>\n\n<p>This is done to move any staging commands out of render passes which is pretty\nmuch required for barrier management (I don\u2019t quite remember though if the\nVulkan validation layer only complained about issuing barriers inside\n<code class=\"language-plaintext highlighter-rouge\">vkBeginRendering\/vkEndRendering<\/code> or if copy commands were also prohibited during\nthe render phase).<\/p>\n\n<p>Long story short: all Vulkan commands used for staging operations are recorded\ninto a separate command buffer so that all GPU =&gt; CPU copies can be moved in\nfront of any computer\/render commands because of various Vulkan API usage\nrestrictions. This was necessary because sokol-gfx allows to call the\nresource update functions at any point in a frame, most importantly within render passes.<\/p>\n\n<h3 id=\"the-resource-barrier-system\">The resource barrier system<\/h3>\n\n<p>This was by far the biggest hassle and took a long time to get right, involving\nseveral rewrites (and there\u2019s <em>still<\/em> quite a lot of room for improvement).<\/p>\n\n<p>The first implementation phase was basically to come up with a general barrier\ninsertion strategy which isn\u2019t completely dumb yet still satisfies the Vulkan\ndefault validation layer, the second and much harder step was then to also satisify\nthe optional synchronization2 validation layer (which even most \u2018official\u2019\nVulkan samples don\u2019t seem to get right - go figure).<\/p>\n\n<p>I won\u2019t bore you with what Vulkan barriers are or why they are necessary, just\nthat barriers are usually needed when a Vulkan buffer or image changes the way\nit is accessed by the GPU (for instance when a resource changes from\nbeing a staging-upload target to being accessed by a shader, or when an image\nobject changes from being used as a pass attachment to being sampled as a\ntexture).<\/p>\n\n<p>In sokol-gfx I tried as much as possible to use a \u2018lazy barrier system\u2019, e.g.\na barrier is inserted at the latest possible moment before a resource is used.<\/p>\n\n<p>The basic idea is that sokol-gfx buffers and images keep track of their current\n\u2018access state\u2019, this may be a combination of:<\/p>\n\n<ul>\n  <li>staging upload target<\/li>\n  <li>vertex buffer binding<\/li>\n  <li>index buffer binding<\/li>\n  <li>read-only storage buffer binding<\/li>\n  <li>read-write storage buffer binding<\/li>\n  <li>texture binding<\/li>\n  <li>storage image binding (always read-write)<\/li>\n  <li>a pass attachment (in the flavours color, resolve, depth or stencil)<\/li>\n  <li>a special \u2018discard\u2019 access modifier for pass attachments\n(used with <code class=\"language-plaintext highlighter-rouge\">SG_LOADACTION_DONTCARE<\/code>)<\/li>\n  <li>swapchain presentation<\/li>\n<\/ul>\n\n<p>Implicity those access states carry additional information which may be needed\nfor picking the right barrier type, like whether shader accesses are read-only,\nread-write or write-only, and whether the access may happen exclusively in\ncompute passes, render passes, or both.<\/p>\n\n<p>Ideally barriers would always be inserted right at the point before a resource\nis bound (because only at that point it\u2019s clear what the new access state is).<\/p>\n\n<p>Unfortunately it\u2019s not that simple: there\u2019s a metric shitton of arbitrary\nrestrictions in Vulkan where exactly barriers may be inserted. The main\nlimitation is that no barriers can be inserted between <code class=\"language-plaintext highlighter-rouge\">vkBeginRendering<\/code> and\n<code class=\"language-plaintext highlighter-rouge\">vkEndRendering<\/code> (which is hella weird, it would be obvious to disallow barriers\nthat involve the current pass attachments, but not for any other resources used\nin the pass).<\/p>\n\n<p>This limitation is currently the main reason why the sokol-gfx barrier system\nis not optimal in some cases, because it requires to move any barriers that would\nbe inserted inside render passes before the start of the render pass. However sokol-gfx\ncan\u2019t predict what resources will actually be used in the render pass\n(spoiler: there\u2019s a surprisingly simple solution to this problem which I\nshould have thought of myself much earlier - but that will be for a later\nVulkan backend update).<\/p>\n\n<p>Currently, barrier insertion points are in the following sokol-gfx functions:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_update\/append_*()<\/code><\/li>\n<\/ul>\n\n<p>The obvious barriers in begin- and end-pass are for image objects transitioning\nin and out of attachment state.<\/p>\n\n<p>In <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> barriers are only inserted inside compute passes (because\nof the above mentioned \u2018no barriers inside render passes\u2019 rule).<\/p>\n\n<p>In staging operations, barriers are issued at the start and end of the staging\noperation, the \u2018after-barrier\u2019 is not great and eventually needs to be\nmoved elsewhere.<\/p>\n\n<p>Now the tricky part: moving barriers out of render passes\u2026 there is one\nsituation where this is relevant: a compute pass writes to a buffer or\nimage, and that buffer or image is then read by a shader in a render pass. Ideally\nthe barrier for this would happen inside the render pass in <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>,\nbut Vulkan validation layer says \u201cno\u201d.<\/p>\n\n<p>What happens instead is that any resource that\u2019s (potentially) written in a\ncompute pass is tracked as \u2018dirty\u2019, and then in the <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code> of the compute\npass, very conservative barriers are inserted for all those dirty resources.\n\u2018Conservative\u2019 means that I cannot predict how the resource will be used next,\nso buffers are generally transitioned into \u2018vertex+index+storage-buffer access\nstate\u2019 and images are generally transferred into \u2018texture access state\u2019.<\/p>\n\n<p>This generally appears to work but is not optimal. We\u2019d like to delay those\nbarriers to when the resources are actually used, and also tighten the scope\nof the barriers to their actual usage.<\/p>\n\n<p>The solution for this is surprisingly simple: use the same \u2018time warp\u2019 that is\nused for recording staging operations by recording barrier commands that would\nneed to be issued from within sokol-gfx render passes into a separate command\nbuffer which can then be enqueued <strong>before<\/strong> another command buffer which holds\nall render\/compute commands for the pass.<\/p>\n\n<p>This is a perfect solution but requires a couple of changes which I didn\u2019t want\nto do in the first Vulkan backend release to not push that out even further:<\/p>\n\n<ul>\n  <li>instead of a single command buffer per frame to hold all render\/compute\ncommands, one command buffer per sokol-gfx pass is needed<\/li>\n  <li>for render passes, a separate command buffer per pass is needed to record\nbarrier commands so that the barriers can be moved out of Vulkan\u2019s\n<code class=\"language-plaintext highlighter-rouge\">vkBeginRendering\/vkEndRendering<\/code><\/li>\n<\/ul>\n\n<p>\u2026inside <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code> we\u2019re now doing some serious\ntime-travelling-shit:<\/p>\n\n<p>Each resource that\u2019s used in a render pass will keep track of all the \u2018access\nstates\u2019 it\u2019s used as in the <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings<\/code> call (for buffers that may be\nvertex-, index- or read-only-storage-buffer-binding and for images it can only\nbe texture-binding), additionally the resource is uniquely-added to a tracking\narray.<\/p>\n\n<p>In <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code> we now have a list of all bound resources and their binding\ntypes, and this information can be used to record \u2018just the right\u2019 barriers into\nthe <strong>separate<\/strong> command buffer that\u2019s been set aside for render pass\nbarriers. This barrier command buffer is then enqueued <strong>before<\/strong> the command\nbuffer which holds the render commands for that pass and voila: perfectly scoped\nrender pass barriers. But as I said, this will need to wait until a followup\nupdate.<\/p>\n\n<h3 id=\"everything-else\">Everything else\u2026<\/h3>\n\n<p>The rest of the Vulkan backend is so straightforward that it\u2019s not\nworth writing about, essentially 1:1 mappings from sokol-gfx API functions\nto Vulkan API functions (the blog post is long enough as it is).<\/p>\n\n<p>Apart from the resource update system (which is overly restrictive and\nconservative in sokol-gfx, mainly because of OpenGL\/WebGL), the sokol-gfx API\nactually is a really good match for Vulkan. There are no expensive operations\n(like creating and discarding Vulkan objects) happening in the \u2018hot-path\u2019. The\nuse of <code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code> is not a great choice for some GPU architectures,\nbut as I said at the start: I\u2019m waiting for Khronos to finish their new resource\nbinding API which apparently will be a mix of D3D12-style descriptor heaps and\n<code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code>.<\/p>\n\n<p>The next steps will most likely be:<\/p>\n\n<ul>\n  <li>porting the backend to Windows (still limited to Intel GPU though)<\/li>\n  <li>port the backend to NVIDIA (will have to wait until around January because\nI\u2019ll be away from my NVIDIA PC for the rest of the year)<\/li>\n  <li>expose a GPU memory allocator interface, and add a sample which hooks up VMA<\/li>\n  <li>\u2026maaaybe integrate SebAaltonen\u2019s OffsetAllocator as default allocator\n(still not clear if I need that when all modern Vulkan drivers no longer\nseem to have that infamous 4096 unique allocations limit)<\/li>\n  <li>tinker around with GPU memory heap types for uniform- and descriptor-buffers\non GPUs without unified memory (e.g. host-visible + device-local)<\/li>\n  <li>figure out why exactly RenderDoc doesn\u2019t work (apparently it\u2019s because\nof <code class=\"language-plaintext highlighter-rouge\">EXT_descriptor_buffer<\/code>, but RenderDoc claims to support the extension since 1.41)<\/li>\n  <li>add support for debug labels (not much point to implement this before\nRenderDoc works)<\/li>\n  <li>implement the improved resource barrier system outlined above<\/li>\n  <li>add support for multiple swapchain passes (not needed when used with sokol_app.h,\nbut required for any \u2018multi-window-scenario\u2019)<\/li>\n  <li>improve interoperability with Vulkan code that exists outside sokol-gfx\n(injecting Vulkan buffers and images into <code class=\"language-plaintext highlighter-rouge\">sg_make_buffer\/sg_make_image<\/code>\nand add the missing <code class=\"language-plaintext highlighter-rouge\">sg_vk_query_*()<\/code> functions to expose internal\nVulkan object handles)<\/li>\n<\/ul>\n\n<p>Originally I also had a long rant about the Vulkan API design in this\nblog post, maybe I\u2019ll put that into a separate post and also\nchange the style from rant into \u2018constructive criticism\u2019 (as hard as that will be lol).<\/p>\n\n<p>My verdict about Vulkan so far is basically: Not great, not terrible.<\/p>\n\n<p>It\u2019s better than OpenGL but not as good (from an API user\u2019s perspective) as pretty\nmuch any other 3D API.  In many places Vulkan is already the same mess as\nOpenGL. Sediment layers of outdated, deprecated or competing features and\nextensions which is incredibly hard to make sense of when not closely following\nVulkan\u2019s development since its initial release in 2016 (which is the exact same\nproblem that ruined OpenGL).<\/p>\n\n<p>At the very least, please, please, PLEASE aggressively remove cruft and reduce\nthe \u2018optional-features creep\u2019 in minor Vulkan API versions (which I think should\nactually be major versions - 4 breaking versions in 10 years sounds just about right).<\/p>\n\n<p>For instance when I\u2019m working against the Vulkan 1.3 API I really don\u2019t care\nabout any legacy features which have been replaced by newer systems (like\nsynchronization2 replacing the old synchronization API). Don\u2019t expose the\nextensions that have been incorporated into core up to 1.3, and also let me filter\nout all those outdated declarations from the Vulkan headers so that code-completion\ndoesn\u2019t suggest outdated API types and functions. Don\u2019t require me to explicitly enable every\nlittle feature (like anisotropic filtering) when creating a Vulkan device. If\nsome shitty old-school GPU doesn\u2019t have anisotropic filtering, then just\nsilently ignore it instead of polluting the 3D API for all eternity just for\nthis one GPU model which probably wasn\u2019t even produced anymore even back in\n2016.<\/p>\n\n<p>Vulkan profiles are a good idea in theory, but please move them into the\ncore API instead of implementing them as a Vulkan SDK feature. Give\nme a <code class=\"language-plaintext highlighter-rouge\">vkCreateSystemDefaultDevice(VK_PROFILE_*)<\/code> function to get rid of those\n500 lines of boilerplate that <strong>every single Vulkan programmer<\/strong> needs to\nduplicate line by line (people who need more control over the setup\nprocess can still use that traditional initialization dance).<\/p>\n\n<p>And PLEASE get somebody into Khronos who has the power to inject at least a\nminimal amount of taste and elegance into Vulkan and who has a clear idea what should\nand shouldn\u2019t go into the core API, because just promoting random vendor\nextensions into core is really not a good way to build an API (and that was\nclear since OpenGL - and the <strong>one<\/strong> thing that Vulkan should have done better).<\/p>\n\n<p>Also, a low-level and explicit API <strong>DOES NOT HAVE TO BE<\/strong> a hassle to use.<\/p>\n\n<p>Somehow modern software systems always seem be built around the \u2018no pain, no\ngain\u2019 philosophy (see Rust, Vulkan, Wayland, \u2026), this sort of self-inflicted\nsuffering for the sake of purity is such a weird Christian flex that\nI\u2019m starting to wonder if \u2018religious memes\u2019 surviving under the surface in even\nthe most rational and atheist developer brains is actually a thing\u2026<\/p>\n\n<p>Maybe we should return to the \u2018Californian hippie attitude\u2019 for building computer\nsystems and software - apparently that had worked pretty great in the 70\u2019s and 80\u2019s ;)<\/p>\n\n<p>\u2026ok I\u2019m getting into old-man-yells-at-cloud-mode again, so I\u2019ll better stop here :D<\/p>\n","pubDate":"Mon, 01 Dec 2025 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2025\/12\/01\/sokol-vulkan-backend-1.html","guid":"https:\/\/floooh.github.io\/2025\/12\/01\/sokol-vulkan-backend-1.html"},{"title":"The sokol-gfx resource view update.","description":"<p><strong>Update:<\/strong> merge happened on 23-Aug-2025.<\/p>\n\n<p>In a couple of days I will merge the next big (and breaking) sokol-gfx\nupdate which adds resource view objects and in turn removes pre-baked\npass-attachment objects.<\/p>\n\n<p>The update also requires to update sokol-shdc and recompile shaders.<\/p>\n\n<p>The root PR is here: <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/1287\">https:\/\/github.com\/floooh\/sokol\/pull\/1287<\/a><\/p>\n\n<p>After merging the update I will spend a couple of weeks to take care of\npending issues and PRs before moving on to a followup <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1302\">resource views update 2<\/a>.<\/p>\n\n<h2 id=\"what-are-resource-view-objects\">What are resource view objects?<\/h2>\n\n<p>If you\u2019re familiar with D3D10 and later you\u2019ll feel right at home since\nresource views are a fundamental concept in D3D, and sokol-gfx\u2019s concept\nof resource views is closest to D3D11. Other 3D APIs either don\u2019t have\nview objects at all (WebGL2 and GL before version 4.3), or only associate resource\nviews with texture data but not buffer data (GL &gt;= 4.3, Metal and WebGPU).<\/p>\n\n<p>Typically resource views have a number of different purposes in the various\n3D-APIs:<\/p>\n\n<ul>\n  <li>they specialize a parent resource object for a specific usage in shaders\n(for instance sampling an image object as a texture versus using the\nsame image object as render target)<\/li>\n  <li>they can reinterpret the data in a resource object (for instance to a\ndifferent pixel format or image type)<\/li>\n  <li>they can define a subset of the data in the resource object (for instance\nselecting a specific mipmap or range of mipmaps in a texture)<\/li>\n<\/ul>\n\n<p>In sokol-gfx you can think of view objects mainly as specializations of an\n<code class=\"language-plaintext highlighter-rouge\">sg_image<\/code> or <code class=\"language-plaintext highlighter-rouge\">sg_buffer<\/code> object for how the image or buffer is going to be accessed in\nshaders:<\/p>\n\n<ul>\n  <li>sampling a texture in a shader requires a <strong>texture view<\/strong><\/li>\n  <li>writing to a storage image in a compute shader requires a <strong>storage image view<\/strong><\/li>\n  <li>accessing a storage buffer in a shader requires a <strong>storage buffer view<\/strong><\/li>\n  <li>each render pass attachment type requires its own view object type:\n    <ul>\n      <li><strong>color-attachment views<\/strong><\/li>\n      <li><strong>resolve-attachment views<\/strong><\/li>\n      <li><strong>depth-stencil-attachment views<\/strong><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>Alternatively you can think of view objects as specializations of a resource\nobject for a specific bindings type (I was actually considering calling this new\nobject type <code class=\"language-plaintext highlighter-rouge\">sg_binding<\/code>, but since \u2018view\u2019 is the more established term I went\nwith <code class=\"language-plaintext highlighter-rouge\">sg_view<\/code> instead).<\/p>\n\n<p>In sokol-gfx, resource view types are \u2018runtime flavours\u2019 of the same handle type\n<code class=\"language-plaintext highlighter-rouge\">sg_view<\/code>. This means that setting the wrong resource type on a bindslot won\u2019t\nbe a compilation error, but a runtime error in the sokol-gfx validation layer,\nso please make sure to test your code in debug build mode from time to time.<\/p>\n\n<h2 id=\"new-unlocked-features\">New unlocked features<\/h2>\n\n<p>This first sokol-gfx resource view update unlocks the following features:<\/p>\n\n<ul>\n  <li>Storage buffer bindings can now have an offset. Binding storage buffers\nwith offsets is mainly useful when the same buffer contains different\ntypes of items in different sections of the buffer, and processing\nthose items in separate compute shaders - or if you only need to access a section\nof a buffer with a compute shader.<\/li>\n  <li>Texture views can define a subset of the parent image by defining\ntheir own mipmap- and slice-ranges (not on WebGL, GLES3 or GL4.1 - e.g. macOS)<\/li>\n  <li>Storage images are no longer \u2018compute pass attachments\u2019, but instead\nbound like regular textures in the <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> call. This\nallows writing to many different storage images in the same compute pass\n(the number of simultaneously bound storage images is still very restricted\nthough)<\/li>\n  <li>Combinations of render pass attachment images are no longer \u2018pre-baked\u2019\ninto <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> objects. Instead <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> is now a\ntransient struct like <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code>. This relaxes another\n\u2018combinatorial explosion scenario\u2019 because rendering code longer needs\nto predict all possible render-pass attachment combinations upfront.<\/li>\n<\/ul>\n\n<h2 id=\"current-restrictions-and-planned-features\">Current restrictions and planned features<\/h2>\n\n<p>The following resource view features are planned for a followup \u2018resource view update 2\u2019:<\/p>\n\n<ul>\n  <li>Reinterpret the pixel format and image type of image objects in a view object.<\/li>\n  <li>Change the max number of per-shader-stage resource bindings of the same type\nfrom hardwired conservative limits to dynamic device limits exposed in the\n<code class=\"language-plaintext highlighter-rouge\">sg_limits<\/code> struct (e.g. more than 4 storage image, 8 storage buffer or 16 texture\nbindings - instead try to push those limits closer to 32)<\/li>\n<\/ul>\n\n<p>For more details about planned \u2018update 2\u2019 features see:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1302\">https:\/\/github.com\/floooh\/sokol\/issues\/1302<\/a><\/p>\n\n<h2 id=\"high-level-overview-of-public-api-changes\">High level overview of public API changes<\/h2>\n\n<ul>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> object type and related functions have been removed<\/li>\n  <li>a new object type <code class=\"language-plaintext highlighter-rouge\">sg_view<\/code> has been added along with related functions<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_features<\/code> gained a new flag <code class=\"language-plaintext highlighter-rouge\">.gl_texture_views<\/code>, when this is false the GL backend doesn\u2019t\nhave full texture view support (e.g. it\u2019s not possible to limit a view to a miplevel or slices\nsubset)<\/li>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> name has been repurposed for a transient struct of render pass\nattachment views:\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_attachments<\/span> <span class=\"p\">{<\/span>\n      <span class=\"n\">sg_view<\/span> <span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_COLOR_ATTACHMENTS<\/span><span class=\"p\">];<\/span>\n      <span class=\"n\">sg_view<\/span> <span class=\"n\">resolves<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_COLOR_ATTACHMENTS<\/span><span class=\"p\">];<\/span>\n      <span class=\"n\">sg_view<\/span> <span class=\"n\">depth_stencil<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span> <span class=\"n\">sg_attachments<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct now has a unified array for views instead of separate\narrays for each \u2018shader resource type\u2019 (textures, storage images and storage buffers):\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_bindings<\/span> <span class=\"p\">{<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n      <span class=\"n\">sg_view<\/span> <span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_VIEW_BINDSLOTS<\/span><span class=\"p\">];<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n  <span class=\"p\">}<\/span> <span class=\"n\">sg_bindings<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">sg_image_usage<\/code> struct now has more detailed usage flags for\nrender pass attachments, and the <code class=\"language-plaintext highlighter-rouge\">.storage_attachment<\/code> usage flag\nhas been renamed to <code class=\"language-plaintext highlighter-rouge\">.storage_image<\/code>:\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_image_usage<\/span> <span class=\"p\">{<\/span>\n      <span class=\"n\">bool<\/span> <span class=\"n\">storage_image<\/span><span class=\"p\">;<\/span>\n      <span class=\"n\">bool<\/span> <span class=\"n\">color_attachment<\/span><span class=\"p\">;<\/span>\n      <span class=\"n\">bool<\/span> <span class=\"n\">resolve_attachment<\/span><span class=\"p\">;<\/span>\n      <span class=\"n\">bool<\/span> <span class=\"n\">depth_stencil_attachment<\/span><span class=\"p\">;<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n  <span class=\"p\">}<\/span> <span class=\"n\">sg_image_usage<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>in <code class=\"language-plaintext highlighter-rouge\">sg_image_desc<\/code> the items to directly inject backend-specific view\nobjects have been removed:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">d3d11_shader_resource_view<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">wgpu_texture_view<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>in <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code>:\n    <ul>\n      <li>the internals of the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct to describe the shader\n  binding interface has been changed to a unified array of <code class=\"language-plaintext highlighter-rouge\">sg_shader_view<\/code>\n  structs:\n        <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_shader_desc<\/span> <span class=\"p\">{<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n      <span class=\"n\">sg_shader_view<\/span> <span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_VIEW_BINDSLOTS<\/span><span class=\"p\">];<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n  <span class=\"p\">}<\/span> <span class=\"n\">sg_shader_desc<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>        <\/div>\n      <\/li>\n      <li>some renaming to better differentiate between \u2018(storage) image and texture\nbindings\u2019, for instance \u2018image-sampler-pairs\u2019 are now called \u2018texture-sampler-pairs\u2019,\nsince only texture bindings are \u2018sampled\u2019, but not storage-image bindings<\/li>\n    <\/ul>\n  <\/li>\n  <li>many new items in the <code class=\"language-plaintext highlighter-rouge\">sg_frame_stats<\/code> struct, mostly not directly related\nto resource views, but filling some gaps<\/li>\n<\/ul>\n\n<h2 id=\"shader-authoring-changes\">Shader Authoring Changes<\/h2>\n\n<blockquote>\n  <p>TL;DR: When recompiling existing shaders you might get new errors about bindslot\ncollisions which need to be resolved by changing the <code class=\"language-plaintext highlighter-rouge\">layout(binding=N)<\/code>\ndecorations.<\/p>\n<\/blockquote>\n\n<p>When using sokol-shdc, the only change on the shader side is that textures,\nstorage buffers and storage images now share a common bindslot range, previously\neach binding type had its own slot range:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">cs<\/span> <span class=\"n\">cs<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">cs_inp_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"n\">rgba8<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">writeonly<\/span> <span class=\"kr\">image2D<\/span> <span class=\"n\">cs_outp_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Note how in this (old) code-snippet the texture- and storage-image bindings use\nthe same bindslot 0 because previously textures and storage images had their own\nbindslot space.<\/p>\n\n<p>This code will now produce a \u2018bindslot collision error\u2019 when compiled with\nsokol-shdc, because texture- and storage-image bindings now use the same bindslot\nspace, so bindings for texture-, storage-buffer- and storage-image-bindings across\nall shader stages need to be fixed to not collide:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">cs<\/span> <span class=\"n\">cs<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">cs_inp_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"n\">rgba8<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">writeonly<\/span> <span class=\"kr\">image2D<\/span> <span class=\"n\">cs_outp_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>This bindslot fixup is the only change required on the shader side.<\/p>\n\n<h2 id=\"working-with-texture-views\">Working with Texture Views<\/h2>\n\n<p>Sample code:<\/p>\n<ul>\n  <li><strong>texcube-sapp<\/strong> (simple textured rendering): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/texcube-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/texcube-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/texcube-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>dyntex-sapp<\/strong> (CPU-update dynamic texture): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/dyntex-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/dyntex-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/dyntex-sapp-ui.html\">WebGPU sample<\/a><\/li>\n<\/ul>\n\n<p>Let\u2019s say a shader defines a texture binding at slot 3:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">tex<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>To \u2018populate\u2019 this bindslot on the CPU side you need two objects now: an image\nobject, and a texture view on the image object:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span><span class=\"p\">.<\/span><span class=\"n\">subimage<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">][<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span> <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Since this is C you can also chain the designated initializers which looks a bit more compact\n(unfortunately this isn\u2019t supported in most other languages):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span> <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> call now has an array of <code class=\"language-plaintext highlighter-rouge\">sg_view<\/code> handles instead\nof separate arrays for images and storage buffers:<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_buffers<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">VIEW_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">tex_view<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span><span class=\"p\">[<\/span><span class=\"n\">SMP_smp<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Since the texture binding was defined as <code class=\"language-plaintext highlighter-rouge\">layout(binding=3)<\/code> it\u2019s also\nsafe to just use the bind slot index directly instead of the code-generated\nconstant:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_buffers<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"mi\">3<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">tex_view<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span><span class=\"p\">[<\/span><span class=\"n\">SMP_smp<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In many situations you only need the view handle and don\u2019t need the\nseparate image handle, this means you can nest the <code class=\"language-plaintext highlighter-rouge\">sg_make_image()<\/code>\ninside the <code class=\"language-plaintext highlighter-rouge\">sg_make_view()<\/code> call:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_view<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">data<\/span><span class=\"p\">.<\/span><span class=\"n\">subimage<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">][<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">}),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>If you need the image handle later you can extract it from the\nview object via <code class=\"language-plaintext highlighter-rouge\">sg_query_view_image()<\/code>:<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_query_view_image<\/span><span class=\"p\">(<\/span><span class=\"n\">tex_view<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Texture views can select a subrange of mipmaps and slices of their parent\nimage (not supported on WebGL2, GLES3 or GL4.1):<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">mip_levels<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">base<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">count<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">3<\/span> <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">slices<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">base<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">5<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">count<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">2<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>If <code class=\"language-plaintext highlighter-rouge\">.count<\/code> is left at default-zero it means \u2018all remaining mipmaps or slices\u2019.\nFor instance this will only skip the most detailed mipmap but keep the\nremaining mipmap chain in place:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">mip_levels<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">base<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">1<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"view-vs-parent-resource-lifetime-considerations\">View vs parent resource lifetime considerations<\/h2>\n\n<p>Before moving on to the other view types, a little interlude about\nlifetimes and resource states:<\/p>\n\n<p>If you\u2019re coming from 3D APIs with ref-counted lifetime management like D3D, WebGPU\nor Metal you might be tempted to \u2018release\u2019 a view\u2019s parent resource object right after creating\nits view object if the image object handle isn\u2019t needed anymore:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span><span class=\"p\">.<\/span><span class=\"n\">subimage<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">][<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span> <span class=\"p\">});<\/span>\n<span class=\"n\">sg_destroy_image<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In sokol-gfx lifetimes are explicit, if you pull the rug under a view\nlike this nothing catastrophic will happen (e.g. no crashes or hard\nvalidation layers errors), but rendering operations involving such \u2018dangling views\u2019\nwill be silently skipped (this is basically the same behavior as before\nwhen trying to render with images or buffers in a non-valid resource state).<\/p>\n\n<p>Another slightly counter-intuitive behavior might be that a view object\nremains in valid resource state despite its parent resource being destroyed, e.g.\nfollowing the above example code:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ get the destroyed image's resource state<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_query_image_state<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"n\">SG_RESOURCESTATE_INVALID<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ if-branch taken, since the image had been destroyed<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"c1\">\/\/ get the image's texture view resource state<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_query_view_state<\/span><span class=\"p\">(<\/span><span class=\"n\">tex_view<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"n\">SG_RESOURCESTATE_VALID<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ if-branch *also* taken!<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>I went a bit back and forth on this decision but I think the behavior makes\nsense from the perspective that all resource state changes in sokol-gfx\nare explicit (e.g. there are no \u2018automatic\u2019 state changes as\na side effect of a \u2018remote\u2019 state change of another object, instead all resource state changes\nare directly caused by a function call on that resource object). The same\nhas always been true for pipelines and their shader object, just not\nspecifically documented.<\/p>\n\n<p>If you want to check whether a view is \u2018renderable\u2019 you can use\nthe following shortcut:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_query_image_state<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_query_view_image<\/span><span class=\"p\">(<\/span><span class=\"n\">tex_view<\/span><span class=\"p\">))<\/span> <span class=\"o\">==<\/span> <span class=\"n\">SG_RESOURCESTATE_VALID<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ the view is 'renderable'<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"c1\">\/\/ or for storage buffer views:<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_query_buffer_state<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_query_view_buffer<\/span><span class=\"p\">(<\/span><span class=\"n\">sbuf_view<\/span><span class=\"p\">))<\/span> <span class=\"o\">==<\/span> <span class=\"n\">SG_RESOURCESTATE_VALID<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ the view is 'renderable'<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>This works because no matter what state the view object is in (or even exists),\n<code class=\"language-plaintext highlighter-rouge\">sq_query_view_image()<\/code> will either return an image handle or an invalid handle\nand both can be passed into <code class=\"language-plaintext highlighter-rouge\">sg_query_image_state()<\/code>. An invalid image handle\nwill return <code class=\"language-plaintext highlighter-rouge\">SG_RESOURCESTATE_INVALID<\/code> while a valid image handle will return\nthe actual <code class=\"language-plaintext highlighter-rouge\">SG_RESOURCESTATE_*<\/code> of the image object.<\/p>\n\n<h2 id=\"tracking-uninit--init-cycles\">Tracking uninit =&gt; init cycles<\/h2>\n\n<p>If the parent resource goes through a \u2018destroy =&gt; make\u2019 or \u2018uninit =&gt; init\u2019 cycle,\nall views which had been created from this parent resource must also be\nre-initialized, otherwise rendering operations involving such \u2018dangling views\u2019\nwill silently be skipped.<\/p>\n\n<p>A common pattern for this situation is to use the \u2018uninit =&gt; init\u2019 calls instead\nof \u2018destroy =&gt; make\u2019 because the handles will remain valid (e.g. you don\u2019t need\nto distribute new object handles into all corners of your code base):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ first uninit\/init the parent image with new params:<\/span>\n<span class=\"n\">sg_uninit_image<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_init_image<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span> <span class=\"p\">...<\/span> <span class=\"p\">});<\/span>\n<span class=\"c1\">\/\/ then 'cycle' the image's view objects<\/span>\n<span class=\"n\">sg_uninit_view<\/span><span class=\"p\">(<\/span><span class=\"n\">tex_view<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_init_view<\/span><span class=\"p\">(<\/span><span class=\"n\">tex_view<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span> <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>I was at first considering to add a \u2018managed mode\u2019 for views which would track\nthe state of their parent resource and automatically go through an uninit\/init\ncycle when needed, but this just didn\u2019t fit into the sokol philosophy of explicit\nlifetimes and resource states, and having this one special case for view objects\ncaused more confusion which wasn\u2019t worth the small gain in convenience (this\ndecision also wasn\u2019t purely based on gut feeling since I actually <em>had<\/em>\nimplemented the \u2018managed mode\u2019 already but then kicked it out again after\nactually starting to port the sokol sample code over - it just didn\u2019t \u2018feel right\u2019).<\/p>\n\n<p>When porting existing code over to resource view objects, don\u2019t forget\nthat you need to destroy at least two objects now for complete cleanup\n(views <em>and<\/em> their parent resource).<\/p>\n\n<p>The order in which you destroy the views and parent resources doesn\u2019t\nmatter, this:<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_destroy_view<\/span><span class=\"p\">(<\/span><span class=\"n\">view<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_destroy_image<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>\u2026works just as well as this:<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_destroy_image<\/span><span class=\"p\">(<\/span><span class=\"n\">img<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_destroy_view<\/span><span class=\"p\">(<\/span><span class=\"n\">view<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p><strong>BUT BE AWARE OF THIS TRAP:<\/strong><\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_destroy_view<\/span><span class=\"p\">(<\/span><span class=\"n\">view<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_destroy_image<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_query_view_image<\/span><span class=\"p\">(<\/span><span class=\"n\">view<\/span><span class=\"p\">));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Since the view is already destroyed, <code class=\"language-plaintext highlighter-rouge\">sg_query_view_image()<\/code> will return the invalid\nhandle, and passing the invalid handle into <code class=\"language-plaintext highlighter-rouge\">sg_destroy_image()<\/code> is a silent no-op\n(e.g. your image will leak).<\/p>\n\n<p>\u2026this is actually a nice example of how convenience in one situation (calling\n<code class=\"language-plaintext highlighter-rouge\">sg_query_view_image(view)<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_destroy_image()<\/code> with an invalid handle\nbeing a silent no-op) can cause trouble in other situations. I\u2019ll need to think\nabout whether this should at least be logged as an error instead.<\/p>\n\n<h2 id=\"working-with-render-pass-attachment-views\">Working with render pass attachment views<\/h2>\n\n<p>Sample code:<\/p>\n\n<ul>\n  <li><strong>offscreen-sapp<\/strong> (simple offscreen rendering): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/offscreen-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/offscreen-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/offscreen-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>offscreen-msaa-sapp<\/strong> (multi-sampled offscreen rendering): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/offscreen-msaa-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/offscreen-msaa-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/offscreen-msaa-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>mrt-sapp<\/strong> (multiple-render-target, multi-sampled offscreen rendering): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/mrt-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/mrt-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/mrt-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>mrt-pixelformats-sapp<\/strong> (multiple render target rendering with different pixel formats): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/mrt-pixelformats-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/mrt-pixelformats-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/mrt-pixelformats-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>shadows-sapp<\/strong> (shadow-mapping with regular shadow map texture): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/shadows-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/shadows-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/shadows-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>shadows-depthtex-sapp<\/strong> (shadow-mapping with a depth-buffer texture): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/shadows-depthtex-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/shadows-depthtex-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/shadows-depthtex-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>miprender-sapp<\/strong> (render into mipmaps): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/miprender-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/miprender-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/miprender-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>layerrender-sapp<\/strong> (render into array slice): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/layerrender-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/layerrender-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/layerrender-sapp-ui.html\">WebGPU sample<\/a><\/li>\n<\/ul>\n\n<p>In the previous sokol-gfx version, when doing offscreen rendering into an image object\na \u2018pre-baked\u2019 attachments object had to be created which was then passed into <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code>:<\/p>\n\n<p>E.g. old code:<\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ create a color and depth-buffer image for offscreen rendering<\/span>\n<span class=\"n\">sg_image<\/span> <span class=\"n\">color_img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">render_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_image<\/span> <span class=\"n\">depth_img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">render_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ create an attachments object from those images...<\/span>\n<span class=\"n\">sg_attachments<\/span> <span class=\"n\">atts<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_attachments<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_attachments_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth_stencil<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">depth_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ ... in the render loop for the offscreen render pass:<\/span>\n<span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"n\">atts<\/span> <span class=\"p\">});<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n\n<span class=\"c1\">\/\/ ... and in the swapchain pass, bind the color image as texture:<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">images<\/span><span class=\"p\">[<\/span><span class=\"n\">TEX_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Now, instead of creating a pre-baked attachments object, separate \u2018attachment-view\u2019\nobjects are created upfront, but their combined use for rendering is no longer\npre-baked but defined on-the-fly in the <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> call, much like\nbindings in the <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> call:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ create color- and depth-buffer images<\/span>\n<span class=\"c1\">\/\/ NOTE the more detailed usage flags<\/span>\n<span class=\"n\">sg_image<\/span> <span class=\"n\">color_img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">color_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_image<\/span> <span class=\"n\">depth_img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ create color- and depth-stencil attachment views<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">color_att_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">color_attachment<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">depth_att_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_attachment<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">depth_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ since the color-attachment image is also sampled as texture,<\/span>\n<span class=\"c1\">\/\/ we'll also need a texture view:<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">color_tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ later in the offscreen render pass, the attachment views<\/span>\n<span class=\"c1\">\/\/ are passed directly into sg_begin_pass:<\/span>\n<span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_att_view<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">depth_stencil<\/span> <span class=\"o\">=<\/span> <span class=\"n\">depth_att_view<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n\n<span class=\"c1\">\/\/ and in the swapchain pass, the texture view is bound<\/span>\n<span class=\"c1\">\/\/ to sample the offscreen-rendered image as texture:<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">VIEW_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_tex_view<\/span><span class=\"p\">,<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"working-with-storage-image-views\">Working with storage image views<\/h2>\n\n<p>Samples:<\/p>\n\n<ul>\n  <li><strong>write-storageimage-sapp<\/strong> (write into storage image with compute shader): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/write-storageimage-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>imageblur-sapp<\/strong> (image blurring with compute shaders): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/imageblur-sapp.html\">WebGPU sample<\/a><\/li>\n<\/ul>\n\n<p>Storage image bindings are no longer defined as compute-pass attachments in <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code>, but instead\nlike regular texture- or storage-buffer-bindings in <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>.<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ first create an image object with storage-image usage:<\/span>\n<span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">storage_image<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ to write to the image with a compute shader, a storage image view is needed:<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">simg_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storage_image<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">mip_level<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>   <span class=\"c1\">\/\/ optional: select a specific miplevel<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">slice<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>       <span class=\"c1\">\/\/ optional: select a specific slice<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ ...and to sample that same image as a texture for rendering, a texture view is needed:<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">tex_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ storage image views are now applied as regular bindings in a compute pass:<\/span>\n<span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">compute<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">});<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">VIEW_simg<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">simg_view<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">})<\/span>\n<span class=\"n\">sg_dispatch<\/span><span class=\"p\">(...);<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n\n<span class=\"c1\">\/\/ and to use the compute-shader-updated image as a texture in a render pass,<\/span>\n<span class=\"c1\">\/\/ bind the texture view as usual:<\/span>\n<span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(...);<\/span>\n<span class=\"c1\">\/\/ ...<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">VIEW_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">tex_view<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span><span class=\"p\">[<\/span><span class=\"n\">SMP_smp<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">smp<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"n\">sg_draw<\/span><span class=\"p\">(...);<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"working-with-storage-buffer-views\">Working with storage buffer views<\/h2>\n\n<p>Samples:<\/p>\n\n<ul>\n  <li><strong>vertexpull-sapp<\/strong> (vertex pulling from storage buffer): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexpull-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexpull-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/vertexpull-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>sbuftex-sapp<\/strong> (access storage buffer in fragment shader): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbuftex-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbuftex-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/sbuftex-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>instancing-compute-sapp<\/strong> (update instancing data with compute shader): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/instancing-compute-sapp-ui.html\">WebGPU sample<\/a><\/li>\n  <li><strong>sbufoffset-sapp<\/strong> (demonstrate storage buffer bindings with offset): <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbufoffset-sapp.c\">C code<\/a>, <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbufoffset-sapp.glsl\">GLSL code<\/a>, <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/sbufoffset-sapp-ui.html\">WebGPU sample<\/a><\/li>\n<\/ul>\n\n<p>To bind a buffer object as storage buffer for vertex-pulling or compute-shader access you now need a storage-buffer-view object:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ create a buffer with storage-buffer usage:<\/span>\n<span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">storage_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">},<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ create a storage buffer view<\/span>\n<span class=\"n\">sg_view<\/span> <span class=\"n\">sbuf_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_view<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_view_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storage_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">buffer<\/span> <span class=\"o\">=<\/span> <span class=\"n\">buf<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">offset<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>  <span class=\"c1\">\/\/ optional 256-byte aligned offset<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"c1\">\/\/ ...later in a render- or compute-pass bind the storage buffer view:<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span><span class=\"p\">[<\/span><span class=\"n\">VIEW_ssbo<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sbuf_view<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The 256-byte-alignment restriction for the offset is a bit unfortunate, since\nvertex-buffer and index-buffer bind offsets don\u2019t have that restriction. The\nalignment restriction is coming in via WebGPU which on some Android devices\nrequires this 256 byte alignment, but the only realistic lower choice would be\n64 bytes which frankly isn\u2019t that much better\n(see: <a href=\"https:\/\/vulkan.gpuinfo.org\/displaydevicelimit.php?platform=android&amp;name=minStorageBufferOffsetAlignment\">https:\/\/vulkan.gpuinfo.org\/displaydevicelimit.php?platform=android&amp;name=minStorageBufferOffsetAlignment<\/a>)\nand would still exclude about 8 percent of Android devices which is quite a lot.<\/p>\n\n<h2 id=\"when-not-using-sokol-shdc\">When not using sokol-shdc\u2026<\/h2>\n\n<p>Samples:<\/p>\n\n<ul>\n  <li>for <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11\">D3D11<\/a><\/li>\n  <li>for <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal\">Metal<\/a><\/li>\n  <li>for <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw\">desktop GL<\/a><\/li>\n  <li>for <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5\">WebGL2<\/a><\/li>\n  <li>for <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu\">WebGPU<\/a><\/li>\n<\/ul>\n\n<p>Some tweaks on the manually populated <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> structs are needed when not\nusing sokol-shdc:<\/p>\n\n<ul>\n  <li>The separate bindslot reflection arrays for images, storage-buffers and storage-images\nhave been unified into a <code class=\"language-plaintext highlighter-rouge\">views[]<\/code> array which mirrors the <code class=\"language-plaintext highlighter-rouge\">views[]<\/code> array in the\n<code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct. The actual reflection information in each view bindslot\nhas remained the same though.<\/li>\n  <li>The <code class=\"language-plaintext highlighter-rouge\">.image_sampler_pair<\/code> array has been renamed to <code class=\"language-plaintext highlighter-rouge\">.texture_sampler_array<\/code>, and\nthe struct member <code class=\"language-plaintext highlighter-rouge\">.image_slot<\/code> has been renamed to <code class=\"language-plaintext highlighter-rouge\">.view_slot<\/code>.<\/li>\n<\/ul>\n\n<p>Example from the <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/mrt-wgpu.c\">wgpu\/mrt_wgpu.c sample<\/a>:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_shader<\/span> <span class=\"n\">fsq_shd<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_shader<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_shader_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"c1\">\/\/ ...<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">views<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">wgsl_group1_binding_n<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span> <span class=\"p\">},<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">].<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">wgsl_group1_binding_n<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">1<\/span> <span class=\"p\">},<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">2<\/span><span class=\"p\">].<\/span><span class=\"n\">texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">wgsl_group1_binding_n<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">2<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">wgsl_group1_binding_n<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">3<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">texture_sampler_pairs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">view_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">sampler_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span> <span class=\"p\">},<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">view_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">sampler_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span> <span class=\"p\">},<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">2<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">stage<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_SHADERSTAGE_FRAGMENT<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">view_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">2<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">sampler_slot<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Shader code changes are only needed on WebGPU when using storage images. Those have\nmoved from <code class=\"language-plaintext highlighter-rouge\">@group(2)<\/code> into <code class=\"language-plaintext highlighter-rouge\">@group(1)<\/code> (this is because storage images are no longer\nspecial compute-pass-attachments, but regular bindings just like texture- and\nstorage-buffer bindings).<\/p>\n\n<h2 id=\"q--a\">Q &amp; A<\/h2>\n\n<h3 id=\"why-no-vertex--and-index-buffer-views\">Why no vertex- and index-buffer views<\/h3>\n\n<p>I had actually implemented vertex- and index-buffer views at first because it\nwould have reduced the size of <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> by 36 bytes (32 bytes vertex-buffer-offsets and 4 bytes\nindex-buffer-offset). In the end I rolled that change back since none of the\nbackend 3D APIs require creating view objects for binding vertex- and index-buffers, but\nsome rendering scenarios (like writing a renderer backend for Dear ImGui) heavily\ndepend on dynamic offsets for vertex- and index-data.<\/p>\n\n<p>I might come back to that idea once additional drawing functions with base-offsets\nare added (which is planned for the \u2018not-too-distant future\u2019). <del>Also adding\na D3D12 backend would require adding view objects for vertex- and index-buffers,\nsince D3D12 has removed the ability to bind vertex- and index-buffers directly\nwith a dynamic offset (at least that\u2019s what I\u2019m seeing in the D3D12 docs).<\/del><\/p>\n\n<p><strong>Update:<\/strong> Nvm, I was wrong here, D3D12 just uses the name \u2018view\u2019 both for transient\nstructs and for baked objects, and <code class=\"language-plaintext highlighter-rouge\">D3D12_VERTEX_BUFFER_VIEW<\/code> and <code class=\"language-plaintext highlighter-rouge\">D3D12_INDEX_BUFFER_VIEW<\/code> are such a transient struct. Thanks to \u2018@lessa@mastodon.gamedev.place` for making me aware of my misconception!<\/p>\n\n<h3 id=\"why-no-texture-field-in-sg_image_usage-to-indicate-that-texture-views-may-be-created-for-an-image-object\">Why no \u2018texture\u2019 field in sg_image_usage to indicate that texture views may be created for an image object?<\/h3>\n\n<p>Simply because creating a texture view is always supported for image objects, so\nthat flag could be implicitly hardwired to true anyway (with one \u2018legacy edge\ncase\u2019: WebGL2 and GL4.1 not supporting binding multi-sampled images as\ntextures). In that edge-case, an explicit <code class=\"language-plaintext highlighter-rouge\">.usage.texture<\/code> flag would allow to fail already at\nimage object creation instead of failing to create a texture view on a\nmulti-sampled image object, but since this is such a minor detail which only affects\n\u2018legacy APIs\u2019 (WebGL2 and GL 4.1) that I didn\u2019t think adding an explicit texture\nusage flag was worth it.<\/p>\n\n<h3 id=\"whats-up-with-sg_max_view_bindslots-being-this-odd-28-instead-of-some-2n-value\">What\u2019s up with SG_MAX_VIEW_BINDSLOTS being this odd 28 instead of some 2^N value?<\/h3>\n\n<p>That way the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct is a nice round 256 bytes (64 bytes for vertex\nbuffer handles and offsets, 8 bytes for index buffer and offset, 112 bytes for\nview handles, 64 bytes for sampler handles plus 2*4 bytes for the start and end\ncanaries).<\/p>\n\n<p>16 separate samplers might be overkill, so I might tweak the number of views vs\nsamplers a bit in the \u2018resource view update 2\u2019.<\/p>\n","pubDate":"Sun, 17 Aug 2025 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2025\/08\/17\/sokol-gfx-view-update.html","guid":"https:\/\/floooh.github.io\/2025\/08\/17\/sokol-gfx-view-update.html"},{"title":"The sokol-gfx 'compute milestone 2' update","description":"<blockquote>\n  <p>Update: merge happened on 24-May-2025<\/p>\n<\/blockquote>\n\n<p>In a couple of days I will merge the next breaking sokol_gfx.h update (aka the <code class=\"language-plaintext highlighter-rouge\">compute-ms2<\/code>\nupdate) which makes working with buffer objects a bit more flexible and will allow\ncompute shaders to write to <code class=\"language-plaintext highlighter-rouge\">sg_image<\/code> objects via \u2018compute pass attachments\u2019.<\/p>\n\n<p>The update also comes with a matching sokol-shdc update which writes additional\nreflection information for storage images used in compute shaders into the\ncode-generated <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct.<\/p>\n\n<blockquote>\n  <p>NOTE: all WASM sample URLs in the blog post require a WebGPU capable browser and will only\nbe valid after the merge.<\/p>\n<\/blockquote>\n\n<p>The implementation ticket is here, and this also has links to all related\nPRs: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1244\">https:\/\/github.com\/floooh\/sokol\/issues\/1244<\/a><\/p>\n\n<h3 id=\"updated-documentation-sections\">Updated documentation sections<\/h3>\n\n<ul>\n  <li>in sokol_gfx.h, re-read the updated section <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/afc74bd88eab597665f5e4f10962c73524d7cbc1\/sokol_gfx.h#L707-L798\">ON COMPUTE PASSES<\/a><\/li>\n  <li>if you\u2019re not using sokol-shdc for shader compilation, also re-read the\nupdated section <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/afc74bd88eab597665f5e4f10962c73524d7cbc1\/sokol_gfx.h#L801-L1036\">ON SHADER CREATION<\/a>\n(most of that information is only needed when <em>not<\/em> using sokol-shdc though)<\/li>\n  <li>read the new doc section <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/afc74bd88eab597665f5e4f10962c73524d7cbc1\/sokol_gfx.h#L1390-L1436\">ON STORAGE IMAGES<\/a><\/li>\n<\/ul>\n\n<h3 id=\"an-important-behaviour-change-for-immutable-buffer-objects\">An important behaviour change for immutable buffer objects<\/h3>\n\n<p>The initial \u2018compute shader\u2019 update allowed to create immutable buffers without\ninitial data and guaranteed that the buffer content would be zero-initialized.\nOn some backend APIs this required a temporary memory allocation of the buffer\nsize which obviously wasn\u2019t great.<\/p>\n\n<p>This guaranteed zero-initialization has been rolled back now and the rules\nfor creating immutable buffer objects have been changed like this:<\/p>\n\n<ul>\n  <li>when creating an immutable non-storage-buffer object (e.g. the buffer cannot\nbe written to with a compute shader), initial data <em>must<\/em> be provided<\/li>\n  <li>when creating an immutable storage-buffer object, no initial data needs to\nprovided, but in that case the buffer content will be \u2018undefined\u2019<\/li>\n<\/ul>\n\n<p>In practice this means that when you use a compute shader to initialize\nstorage buffer content you can no longer rely on the initial buffer content being\nzero-initialized, instead write <em>all<\/em> buffer items in the compute shader,\neven when they are supposed to be zero.<\/p>\n\n<h3 id=\"multi-purpose-buffer-objects\">Multi-purpose buffer objects<\/h3>\n\n<p>It\u2019s now possible to bind the same buffer object to different bind points (e.g.\nbind the same buffer as vertex buffer, index buffer and\/or storage buffer).\nThis means the following scenarios are now enabled:<\/p>\n\n<ul>\n  <li>It\u2019s possible to stash vertices and indices into the same buffer\n(with the exception of WebGL2 where this is explicitly disallowed)<\/li>\n  <li>It\u2019s now possible to use a compute shader to write data to a buffer, and\nthen bind this buffer as vertex- or index-buffer.<\/li>\n<\/ul>\n\n<p>To achieve this, the <code class=\"language-plaintext highlighter-rouge\">sg_buffer_desc<\/code> struct has been changed to merge the previous\nbuffer type and buffer usage enum items into a new <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code> struct which is a\nboolean flag group:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_buffer_usage<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">vertex_buffer<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">index_buffer<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">storage_buffer<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">immutable<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">dynamic_update<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">stream_update<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sg_buffer_usage<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The default setup configures an immutable vertex buffer (just as before), e.g.\ncreating a buffer object like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices<\/span><span class=\"p\">),<\/span>\n<span class=\"p\">})<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026is identical with:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">immutable<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices<\/span><span class=\"p\">),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to create an immutable index buffer:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">indices<\/span><span class=\"p\">),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to create an index buffer with stream-update hint:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">stream_update<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">size<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to create a buffer that can be written by a compute shader and then\nbound to a vertex buffer bindpoint:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">storage_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">size<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and the same as index buffer:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">storage_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">size<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>To stash both vertices and indices into the same buffer object:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices_and_indices<\/span><span class=\"p\">),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Note that \u2018multi-purpose buffer usage\u2019 is explicitly disallowed on WebGL2 (which is\nonly relevant for using a single buffer to hold vertex- and index-data, since\nstorage buffers are not available on WebGL2 anyway). To check for this restriction\nuse the new <code class=\"language-plaintext highlighter-rouge\">sg_features.separate_buffer_types<\/code> boolean:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"n\">sg_query_features<\/span><span class=\"p\">().<\/span><span class=\"n\">separate_buffer_types<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">const<\/span> <span class=\"n\">sg_buffer<\/span> <span class=\"n\">buf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices_and_indices<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">});<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Any invalid combination of usage flags will also be checked in the sokol-gfx validation layer.<\/p>\n\n<p>The following new sample uses a combined vertex\/index buffer:<\/p>\n\n<ul>\n  <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexindexbuffer-sapp.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexindexbuffer-sapp.c<\/a><\/li>\n  <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexindexbuffer-sapp.glsl\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexindexbuffer-sapp.glsl<\/a><\/li>\n  <li>WASM: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/vertexindexbuffer-sapp-ui.html\">https:\/\/floooh.github.io\/sokol-webgpu\/vertexindexbuffer-sapp-ui.html<\/a><\/li>\n<\/ul>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">instancing-compute-sapp<\/code> sample has been updated to bind the compute-shader-updated\nstorage buffer as vertex buffer with hardware instancing:<\/p>\n\n<ul>\n  <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.c<\/a><\/li>\n  <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.glsl\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.glsl<\/a><\/li>\n  <li>WASM: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/instancing-compute-sapp-ui.html\">https:\/\/floooh.github.io\/sokol-webgpu\/instancing-compute-sapp-ui.html<\/a><\/li>\n<\/ul>\n\n<p>There is no sample yet which uses a compute shader to write index data.<\/p>\n\n<h3 id=\"breaking-changes-when-creating-image-objects\">Breaking changes when creating image objects<\/h3>\n\n<p>Similar to the above <code class=\"language-plaintext highlighter-rouge\">sg_buffer_desc<\/code> change, usage hints in the <code class=\"language-plaintext highlighter-rouge\">sg_image_desc<\/code> struct\nare now provided through a new <code class=\"language-plaintext highlighter-rouge\">sg_image_usage<\/code> struct looking like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_image_usage<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">render_attachment<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">storage_attachment<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">immutable<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">dynamic_update<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">bool<\/span> <span class=\"n\">stream_update<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sg_image_usage<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>E.g. creating a \u2018render-target texture\u2019 for offscreen rendering now looks like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">render_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and creating a image updated dynamically with CPU data with stream-update\nbehaviour:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">stream_update<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>As with <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code>, invalid usage flag combinations are caught in the\nsokol-gfx validation layer.<\/p>\n\n<h3 id=\"compute-pass-attachments-aka-storage-images\">Compute pass attachments (aka storage images)<\/h3>\n\n<p>It\u2019s now possible to use compute shaders to write to <code class=\"language-plaintext highlighter-rouge\">sg_image<\/code> objects. The way this is currently\nimplemented is very similar to offscreen rendering (but will change in a future \u2018resource view update\u2019,\nmore info on that at the end of the blog post).<\/p>\n\n<p>Let\u2019s first write a simple compute shader in the sokol-shdc GLSL flavour which writes some\nanimated color gradient to a storage image:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">cs<\/span> <span class=\"n\">cs<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">cs_params<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">float<\/span> <span class=\"n\">offset<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">};<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"n\">rgba8<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">writeonly<\/span> <span class=\"kr\">image2D<\/span> <span class=\"n\">cs_out_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">local_size_x<\/span><span class=\"o\">=<\/span><span class=\"mi\">16<\/span><span class=\"p\">,<\/span> <span class=\"n\">local_size_y<\/span><span class=\"o\">=<\/span><span class=\"mi\">16<\/span><span class=\"p\">)<\/span> <span class=\"k\">in<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kt\">void<\/span> <span class=\"nf\">main<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">ivec2<\/span> <span class=\"n\">size<\/span> <span class=\"o\">=<\/span> <span class=\"n\">imageSize<\/span><span class=\"p\">(<\/span><span class=\"n\">cs_out_tex<\/span><span class=\"p\">);<\/span>\n    <span class=\"kt\">ivec2<\/span> <span class=\"n\">pos<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">ivec2<\/span><span class=\"p\">(<\/span><span class=\"n\">mod<\/span><span class=\"p\">(<\/span><span class=\"kt\">vec2<\/span><span class=\"p\">(<\/span><span class=\"n\">gl_GlobalInvocationID<\/span><span class=\"p\">.<\/span><span class=\"n\">xy<\/span><span class=\"p\">)<\/span> <span class=\"o\">+<\/span> <span class=\"kt\">vec2<\/span><span class=\"p\">(<\/span><span class=\"n\">size<\/span><span class=\"p\">)<\/span> <span class=\"o\">*<\/span> <span class=\"n\">offset<\/span><span class=\"p\">,<\/span> <span class=\"n\">size<\/span><span class=\"p\">));<\/span>\n    <span class=\"kt\">vec4<\/span> <span class=\"n\">color<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">vec4<\/span><span class=\"p\">(<\/span><span class=\"kt\">vec2<\/span><span class=\"p\">(<\/span><span class=\"n\">gl_GlobalInvocationID<\/span><span class=\"p\">.<\/span><span class=\"n\">xy<\/span><span class=\"p\">)<\/span> <span class=\"o\">\/<\/span> <span class=\"kt\">float<\/span><span class=\"p\">(<\/span><span class=\"n\">size<\/span><span class=\"p\">),<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">);<\/span>\n    <span class=\"n\">imageStore<\/span><span class=\"p\">(<\/span><span class=\"n\">cs_out_tex<\/span><span class=\"p\">,<\/span> <span class=\"n\">pos<\/span><span class=\"p\">,<\/span> <span class=\"n\">color<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">program<\/span> <span class=\"n\">compute<\/span> <span class=\"n\">cs<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>On the CPU side, create an <code class=\"language-plaintext highlighter-rouge\">sg_image<\/code> object with \u2018storage attachment usage\u2019:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_image<\/span> <span class=\"n\">img<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_image<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_image_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">usage<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">storage_attachment<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"n\">WIDTH<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"n\">HEIGHT<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">pixel_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_RGBA8<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Next the image must be wrapped in an <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> object. This allows to pick a specific\nimage surface (mip-level and\/or slice) for the compute shader to access.\nUp to 4 (or <code class=\"language-plaintext highlighter-rouge\">SG_MAX_STORAGE_ATTACHMENTS<\/code>) images can be defined in a single attachment:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_attachments<\/span> <span class=\"n\">atts<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_attachments<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_attachments_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storages<\/span><span class=\"p\">[<\/span><span class=\"n\">SIMG_cs_out_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">img<\/span><span class=\"p\">,<\/span>\n        <span class=\"c1\">\/\/ optionally pick a mip level and slice:<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">mip_level<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">slice<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026next a compute pipeline object which wraps the above compute shader:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_pipeline<\/span> <span class=\"n\">pip<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_pipeline<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pipeline_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">compute<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">shader<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_shader<\/span><span class=\"p\">(<\/span><span class=\"n\">compute_shader_desc<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_query_backend<\/span><span class=\"p\">)),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In the frame loop, run a compute pass and provide the attachments object,\napply the compute pipeline and uniform data, and finally call <code class=\"language-plaintext highlighter-rouge\">sg_dispatch()<\/code>\nto kick off the compute shader:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">compute<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"n\">atts<\/span> <span class=\"p\">});<\/span>\n<span class=\"n\">sg_apply_pipeline<\/span><span class=\"p\">(<\/span><span class=\"n\">pip<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(<\/span><span class=\"n\">UB_cs_params<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">cs_params<\/span><span class=\"p\">));<\/span>\n<span class=\"n\">sg_dispatch<\/span><span class=\"p\">(<\/span><span class=\"n\">WIDTH<\/span> <span class=\"o\">\/<\/span> <span class=\"mi\">16<\/span><span class=\"p\">,<\/span> <span class=\"n\">HEIGHT<\/span> <span class=\"o\">\/<\/span> <span class=\"mi\">16<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026after the compute pass the image object can then be used as a texture binding in a regular render pass.<\/p>\n\n<p>Find the complete sample here:<\/p>\n\n<ul>\n  <li>WASM: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/write-storageimage-sapp.html\">https:\/\/floooh.github.io\/sokol-webgpu\/write-storageimage-sapp.html<\/a><\/li>\n  <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.c<\/a><\/li>\n  <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.glsl\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/write-storageimage-sapp.glsl<\/a><\/li>\n<\/ul>\n\n<p>\u2026and a more advanced example which has been ported from WebGPU:<\/p>\n\n<ul>\n  <li>WASM: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/imageblur-sapp.html\">https:\/\/floooh.github.io\/sokol-webgpu\/imageblur-sapp.html<\/a><\/li>\n  <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.c<\/a><\/li>\n  <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/imageblur-sapp.c<\/a><\/li>\n<\/ul>\n\n<h3 id=\"detailed-change-list\">Detailed change list<\/h3>\n\n<h4 id=\"sokol_apph\">sokol_app.h:<\/h4>\n\n<p>The D3D11\/DXGI backend now creates a <code class=\"language-plaintext highlighter-rouge\">D3D_FEATURE_LEVEL_11_1<\/code> device (with a\nfallback to <code class=\"language-plaintext highlighter-rouge\">D3D_FEATURE_LEVEL_11_0<\/code>). Feature Level 11.1 is needed to allow\nmore than 8 UAV (Unordered Access View) bindings. D3D11.1 was released around\n2011 with Windows 8, so this is only an issue if support for Windows 7 is still\nrequired or on very old GPUs (Win7 is now at 0.12% on Steam Hardware Survey,\nbut even if this turns out to be a problem, only the bindslot allocation\nstrategy in sokol-shdc for HLSL5 UAV bindslots needs to be changed).<\/p>\n\n<h4 id=\"sokol_gfxh\">sokol_gfx.h:<\/h4>\n\n<ul>\n  <li>A new constant <code class=\"language-plaintext highlighter-rouge\">SG_MAX_STORAGE_ATTACHMENTS = 4<\/code> has been added (most likely\nbumped to at least 8 in the future)<\/li>\n  <li>The struct <code class=\"language-plaintext highlighter-rouge\">sg_pixelformat_info<\/code> has gained two new flags:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">bool read<\/code>: true if the pixel format supports compute shader read access<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">bool write<\/code>: true if the pixel format supports compute shader write access<\/li>\n    <\/ul>\n\n    <p>Currently the list of compute shader accessible pixel formats is hardwired to\nthe following list which is safe to use across all GPUs and backend APIs\n(all those formats support read+write access):<\/p>\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RGBA8<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RGBA8SN\/UI\/SI<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RGBA16UI\/SI\/F<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_R32UI\/SI\/F<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RG32UI\/SI\/F<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RGBA32UI\/SI\/F<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>A new feature flag <code class=\"language-plaintext highlighter-rouge\">sg_features.separate_buffer_types<\/code> has been added,\nthis is only true on WebGL2. The only effect of that flag is that\nthe same buffer object cannot be used as vertex- and index-buffer bindings.<\/li>\n  <li>The enums <code class=\"language-plaintext highlighter-rouge\">sg_usage<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_buffer_type<\/code> have been removed.<\/li>\n  <li>The struct <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code> has been added.<\/li>\n  <li>The enum field <code class=\"language-plaintext highlighter-rouge\">sg_buffer_desc.type<\/code> has been removed and replaced by\nboolean flags in <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code>.<\/li>\n  <li>The enum field <code class=\"language-plaintext highlighter-rouge\">sg_buffer_desc.usage<\/code> has been repurposed as nested\nstruct item of type <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code>.<\/li>\n  <li>The struct <code class=\"language-plaintext highlighter-rouge\">sg_image_usage<\/code> has been added.<\/li>\n  <li>The boolean <code class=\"language-plaintext highlighter-rouge\">sg_image_desc.render_target<\/code> has been removed and replaced\nby <code class=\"language-plaintext highlighter-rouge\">sg_image_usage.render_attachment<\/code><\/li>\n  <li>The enum feld <code class=\"language-plaintext highlighter-rouge\">sg_image_desc.usage<\/code> has been repurposed as nested struct\nitem of type <code class=\"language-plaintext highlighter-rouge\">sg_image_usage<\/code>.<\/li>\n  <li>A new struct <code class=\"language-plaintext highlighter-rouge\">sg_shader_storage_image<\/code> has been added, this is nested in\nin <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> and holds reflection information about storage image\nbindings in compute shaders.<\/li>\n  <li>A new array <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc.storage_images[]<\/code> has been added to communicate\nreflection information about storage image usage in compute shaders to sokol_gfx.h<\/li>\n  <li>A new array <code class=\"language-plaintext highlighter-rouge\">sg_attachments_desc.storages[]<\/code> has been added to describe\n\u2018storage image attachments\u2019 for compute passes.<\/li>\n  <li>The function <code class=\"language-plaintext highlighter-rouge\">sg_query_buffer_usage()<\/code> now returns a struct <code class=\"language-plaintext highlighter-rouge\">sg_buffer_usage<\/code>.<\/li>\n  <li>The function <code class=\"language-plaintext highlighter-rouge\">sg_query_image_usage()<\/code> now returns a struct <code class=\"language-plaintext highlighter-rouge\">sg_image_usage<\/code>.<\/li>\n<\/ul>\n\n<h3 id=\"whats-next\">What\u2019s next<\/h3>\n\n<p>Long story short: while working on the storage image update it became clear\nthat sokol_gfx.h needs resource-view objects.<\/p>\n\n<p>This will allow more flexible resource bindings without creating temporary\n3D-backend objects in the \u2018hot path\u2019 while keeping the sokol_gfx.h backend\nimplementations simple (e.g. I want to avoid a dynamic \u2018hash-and-cache\u2019\napproach for 3D-backend resource objects as much as possible, it\u2019s already bad\nenough that this is needed with WebGPU BindGroups).<\/p>\n\n<p>Currently resource view objects are managed under the hood, for instance\nin the D3D11 backend:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_buffer<\/code> objects with storage buffer usage generally create a Shader Resource View\nfor readonly-access in vertex-, fragment- and compute-shaders, and if the buffer\nis immutable, also an Unordered Access View for write-access in compute shaders.\nNotably, any starting offsets are hardwired to zero in both view objects.<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_image<\/code> objects generally create a Shader Resource View object, but without\nallowing to specify a mip-level range, array-slice range or different pixel format.<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> objects create:\n    <ul>\n      <li>one Render Target View object per color attachment<\/li>\n      <li>an optional Depth Stencil View object for the depth-stencil attachment<\/li>\n      <li>one Unordered Access View object per storage attachment<\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>The reason why storage images are currently treated as pass attachments instead\nof regular bindings applied via <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> is because storage image\nbindings need to pick a mip-level and\/or slice, and at least on D3D11 this\nrequires a baked UAV object. Likewise, binding the same storage buffer with\ndifferent offsets would require one SRV or UAV object per offset.<\/p>\n\n<p>The current plan for view objects in sokol_gfx.h looks like this:<\/p>\n\n<ul>\n  <li>a single new resource object type is added: <code class=\"language-plaintext highlighter-rouge\">sg_view<\/code>, with matching structs and functions\n(<code class=\"language-plaintext highlighter-rouge\">sg_view_desc<\/code>, <code class=\"language-plaintext highlighter-rouge\">sg_make_view()<\/code>, <code class=\"language-plaintext highlighter-rouge\">sg_destroy_view()<\/code>, etc\u2026)<\/li>\n  <li>in return, the <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> resource object type is removed (along with <code class=\"language-plaintext highlighter-rouge\">sg_attachments_desc<\/code>,\n<code class=\"language-plaintext highlighter-rouge\">sg_make_attachments()<\/code>, <code class=\"language-plaintext highlighter-rouge\">sg_destroy_attachments()<\/code> etc\u2026)<\/li>\n  <li>view objects can be thought of as specialization of a resource object for\na specific bindslot type (I actually thought about calling the new resource type <code class=\"language-plaintext highlighter-rouge\">sg_binding<\/code>,\nbut \u2018view\u2019 is the established name for this type of thing across backend 3D APIs), e.g. views will come in the\nfollowing \u2018runtime flavours\u2019:\n    <ul>\n      <li>texture views<\/li>\n      <li>storage buffer views<\/li>\n      <li>storage image views<\/li>\n      <li>color attachment views<\/li>\n      <li>resolve attachment views<\/li>\n      <li>depth-stencil attachment views<\/li>\n    <\/ul>\n  <\/li>\n  <li>\u2026and maybe (but not sure yet):\n    <ul>\n      <li>vertex buffer views<\/li>\n      <li>index buffer views<\/li>\n    <\/ul>\n\n    <p>\u2026vertex- and index-buffer-views would allow to remove the bind offset for\nvertex- and index-buffers from <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code>, with the downside that one view\nobject would be required per offset, but I can\u2019t think of a situation where a\nhighly dynamic starting offset would be required for vertex- and index-data.\nTo be clear: there is no backend API which requires a view object for vertex-\nand index-buffer bindings, it would be purely a sokol_gfx.h thing (this also\nmeans that it would be very cheap to build and destroy vertex- and\nindex-buffer-view objects on the fly since no calls into backend APIs would happen)<\/p>\n  <\/li>\n  <li>\n    <p>the new <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct would then look like this (notably storage\nimages for compute shader access would move from \u2018pass attachments\u2019 to\nregular \u2018bindings\u2019)<\/p>\n\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_bindings<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">vertex_buffers<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_VERTEXBUFFER_BINDINGS<\/span><span class=\"p\">]<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">index_buffer<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">textures<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_TEXTURE_BINDINGS<\/span><span class=\"p\">];<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">storage_buffers<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_STORAGEBUFFER_BINDINGS<\/span><span class=\"p\">];<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">storage_images<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_STORAGEIMAGE_BINDINGS<\/span><span class=\"p\">]<\/span>\n    <span class=\"n\">sg_sampler<\/span> <span class=\"n\">samplers<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_SAMPLER_BINDINGS<\/span><span class=\"p\">];<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sg_bindings<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>\n    <p><code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> would become a \u2018transient struct\u2019 similar to\n<code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code>:<\/p>\n\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_attachments<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_COLOR_ATTACHMENTS<\/span><span class=\"p\">];<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">resolves<\/span><span class=\"p\">[<\/span><span class=\"n\">SG_MAX_COLOR_ATTACHMENTS<\/span><span class=\"p\">];<\/span>\n    <span class=\"n\">sg_view<\/span> <span class=\"n\">depth_stencil<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sg_attachments<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n<\/ul>\n\n<p>This \u2018view update\u2019 would have the following advantages:<\/p>\n\n<ul>\n  <li>storage buffer bindings can have a starting offset, which simplifies\nmanaging different types of data in the same buffer<\/li>\n  <li>texture and storage image bindings can (to some extent) reinterpret\nthe image data (e.g. casting to a different pixel format or selecting\na miplevel and slice range - this will have to be behind a feature flag\nthough)<\/li>\n  <li>multiple-render-target combinations no longer need to be prebaked<\/li>\n<\/ul>\n\n<p>No ETA yet on the \u2018view update\u2019 though, first I want to fix a couple of\ninternal things:<\/p>\n\n<ul>\n  <li>the GL texture creation code is currently an unholy combination of\n<code class=\"language-plaintext highlighter-rouge\">glTexStorage<\/code> and <code class=\"language-plaintext highlighter-rouge\">glTexImage<\/code> functions. I want to cleanly split\nthis into two code paths (unfortunatly macOS being stuck at GL 4.1\ndoesn\u2019t have the <code class=\"language-plaintext highlighter-rouge\">glTexStorage<\/code> functions, although I heard\nthat those functions are implemented but just not present in the\ncore GL headers - which I\u2019ll need to investigate)<\/li>\n  <li>I want to improve the internal \u2018lifetime tracking\u2019 for referenced\nresources (e.g. one resource object holding a reference to another\nobject). Currently it\u2019s not possible to detect when such a referenced\nobject has gone through an \u2018uninit\/init\u2019 cycle because this keeps\nthe same public handle while discarding and recreating backend\n3D API objects. Especially for view objects (which need to track\ntheir original resource object) it is important that views can\ndetect when their referenced resource object is discarded (and\nI\u2019m thinking about \u2018auto-managed\u2019 view objects which can recreate\nthemselves on the fly when their resource object goes through\nuninit\/init - no promises yet though).<\/li>\n<\/ul>\n\n<p>More info on those planned updates are in the following planning\ntickets:<\/p>\n\n<ul>\n  <li>resource views: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1252\">https:\/\/github.com\/floooh\/sokol\/issues\/1252<\/a><\/li>\n  <li>better internal reference tracking: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1260\">https:\/\/github.com\/floooh\/sokol\/issues\/1260<\/a><\/li>\n  <li>glTexStorage vs glTexImage: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1263\">https:\/\/github.com\/floooh\/sokol\/issues\/1263<\/a><\/li>\n<\/ul>\n\n<p>\u2026and that is all for today :)<\/p>\n","pubDate":"Mon, 19 May 2025 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2025\/05\/19\/sokol-gfx-compute-ms2.html","guid":"https:\/\/floooh.github.io\/2025\/05\/19\/sokol-gfx-compute-ms2.html"},{"title":"The sokol-gfx compute shader update","description":"<p><strong>Update:<\/strong> merged happened on 08-Mar-2025<\/p>\n\n<p>In the next couple of days I will merge initial compute shader support\nfor sokol_gfx.h (and sokol-shdc). The update is surprisingly \u2018low-profile\u2019 in terms\nof API changes, the only breaking change is that the runtime feature flag\n<code class=\"language-plaintext highlighter-rouge\">sg_features.storage_buffer<\/code> has been renamed to <code class=\"language-plaintext highlighter-rouge\">sg_features.compute<\/code>\n(this is because the same backends that supported storage buffers before\nnow also support compute shaders).<\/p>\n\n<h2 id=\"availability-and-restrictions\">Availability and Restrictions<\/h2>\n\n<p>Compute shader support is available on the following platform\/backend combos:<\/p>\n\n<ul>\n  <li>macOS and iOS with Metal<\/li>\n  <li>Windows with D3D11 and GL<\/li>\n  <li>Linux with GL<\/li>\n  <li>Web with WebGPU<\/li>\n<\/ul>\n\n<p>\u2026which means that compute shaders are not available on:<\/p>\n\n<ul>\n  <li>macOS with GL<\/li>\n  <li>iOS with GLES3<\/li>\n  <li>Web with WebGL2<\/li>\n  <li>Android with GLES3<\/li>\n<\/ul>\n\n<p>The initial compute shader support comes with a couple of restricitions\nwhich will most likely be lifted in later updates (in about that order):<\/p>\n\n<ul>\n  <li>storage buffers cannot be bound as vertex- or index-buffers<\/li>\n  <li>no storage textures, e.g. compute shaders can only write buffer data but not texture data<\/li>\n  <li>there\u2019s no way to read data from GPU resources back to the CPU side (or\ncopy data between GPU resources)<\/li>\n<\/ul>\n\n<p>Right now compute shaders are mostly useful for replacing\ndynamic- and streaming-buffer update scenarios, where dynamic render\ndata is computed on the CPU and uploaded to buffers via <code class=\"language-plaintext highlighter-rouge\">sg_update_buffer()<\/code>.<\/p>\n\n<h2 id=\"new-compute-shader-samples\">New compute shader samples<\/h2>\n\n<p>To get an idea how compute shaders work in sokol-gfx, it\u2019s best to read the\nnew sample code:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.c\">C code<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-compute-sapp.glsl\">GLSL code<\/a><\/li>\n  <li><a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/instancing-compute-sapp.html\">WebGPU demo<\/a><\/li>\n<\/ul>\n\n<p>This is an evolution of the <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/instancing-sapp-ui.html\">instancing-sapp<\/a>\nsample, and moves all particle computations into compute shaders.<\/p>\n\n<p>The other compute shader sample is a straight port of the <a href=\"https:\/\/webgpu.github.io\/webgpu-samples\/?sample=computeBoids\">WebGPU compute boids sample<\/a> to\nsokol-gfx:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/computeboids-sapp.c\">C code<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/computeboids-sapp.glsl\">GLSL code<\/a><\/li>\n  <li><a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/computeboids-sapp.html\">WebGPU demo<\/a><\/li>\n<\/ul>\n\n<p>Those two samples use \u2018cross-backend\u2019 GLSL shader code compiled to the underlying\nshading languages via <a href=\"https:\/\/github.com\/floooh\/sokol-tools\/\">sokol-shdc<\/a>.<\/p>\n\n<p>For authoring compute shaders with sokol-shdc it might make sense to read up\non <a href=\"https:\/\/www.khronos.org\/opengl\/wiki\/Compute_Shader\">GLSL compute shaders in the GL Wiki<\/a> -\nnote though that not all features have been properly tested yet (like sampling\ntextures in compute shaders, or accessing shared memory).<\/p>\n\n<p>For using sokol-gfx compute shaders without sokol-shdc, check out the following\nbackend specific versions of the <code class=\"language-plaintext highlighter-rouge\">instancing-compute<\/code> sample:<\/p>\n\n<ul>\n  <li>D3D11: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/d3d11\/instancing-compute-d3d11.c\">instancing-compute-d3d11.c<\/a><\/li>\n  <li>Metal: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/metal\/instancing-compute-metal.c\">instancing-compute-metal.c<\/a><\/li>\n  <li>WebGPU: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/instancing-compute-wgpu.c\">instancing-compute-wgpu.c<\/a><\/li>\n  <li>GL4.3: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/instancing-compute-glfw.c\">instancing-compute-glfw.c<\/a><\/li>\n<\/ul>\n\n<p>Also check out the updated documentation of <a href=\"https:\/\/github.com\/floooh\/sokol-tools\/blob\/master\/docs\/sokol-shdc.md\">sokol-shdc<\/a>,\nand the new documentation comment section on compute shaders in the sokol_gfx.h\nheader (search for: <code class=\"language-plaintext highlighter-rouge\">ON COMPUTE PASSES<\/code> and re-read the updated section <code class=\"language-plaintext highlighter-rouge\">ON SHADER CREATION<\/code>).<\/p>\n\n<h2 id=\"shader-authoring-changes\">Shader Authoring Changes<\/h2>\n\n<p>The sokol-gfx update comes with a matching sokol-shdc update for\nauthoring compute shaders.<\/p>\n\n<p>A new tag <code class=\"language-plaintext highlighter-rouge\">@cs [name]<\/code> (similar to the existing <code class=\"language-plaintext highlighter-rouge\">@vs [name]<\/code> and <code class=\"language-plaintext highlighter-rouge\">@fs [name]<\/code>)\nis used to identify a compute shader snippet, e.g. everything inside <code class=\"language-plaintext highlighter-rouge\">@cs \/ @end<\/code>\nwill be compiled as a <a href=\"https:\/\/www.khronos.org\/opengl\/wiki\/Compute_Shader\">GLSL compute shader<\/a>.<\/p>\n\n<p>NOTE that the distinction between readonly and read\/write storage buffer\nbindings is important, e.g.:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"n\">readonly<\/span> <span class=\"n\">buffer<\/span> <span class=\"n\">cs_ssbo_in<\/span> <span class=\"p\">{<\/span> <span class=\"n\">particle<\/span> <span class=\"n\">prt_in<\/span><span class=\"p\">[];<\/span> <span class=\"p\">};<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"n\">buffer<\/span> <span class=\"n\">cs_ssbo_out<\/span> <span class=\"p\">{<\/span> <span class=\"n\">particle<\/span> <span class=\"n\">prt_out<\/span><span class=\"p\">[];<\/span> <span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>If your compute shader only reads (but doesn\u2019t write) storage buffer content,\nits binding declaration should be marked as <code class=\"language-plaintext highlighter-rouge\">readonly<\/code>. This information will\nbe extracted by sokol-shdc and used by sokol-gfx for hazard-tracking\nneeded in some 3D-APIs.<\/p>\n\n<p>The other notable shader specialty is the \u2018workgroup size\u2019, which in\nGLSL is defined as:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">local_size_x<\/span><span class=\"o\">=<\/span><span class=\"n\">X<\/span><span class=\"p\">,<\/span> <span class=\"n\">local_size_y<\/span><span class=\"o\">=<\/span><span class=\"n\">Y<\/span><span class=\"p\">,<\/span> <span class=\"n\">local_size_z<\/span><span class=\"o\">=<\/span><span class=\"n\">Z<\/span><span class=\"p\">)<\/span> <span class=\"k\">in<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026if you\u2019re used to HLSL, this is the same as <code class=\"language-plaintext highlighter-rouge\">[numthreads(X,Y,Z)]<\/code>, or in WGSL\n<code class=\"language-plaintext highlighter-rouge\">@workgroup_size(X,Y,Z)<\/code>. On Metal this is called <code class=\"language-plaintext highlighter-rouge\">threadsPerThreadGroup<\/code> and\nis <strong>not<\/strong> defined in the shader code, but on the CPU side when issuing a dispatch\ncall (this is another case where sokol-shdc comes in handy, since it extracts\nthe workgroup size from the GLSL shader and passes it into sokol-gfx as\n<code class=\"language-plaintext highlighter-rouge\">sg_shader_desc.mtl_threads_per_threadgroup<\/code>).<\/p>\n\n<p>Other then that you mainly need to be aware that your compute shader code must\nbe thread safe because compute shaders allow random write access into storage buffers\nand the GPU is spawning many invocations of your shader running in parallel.<\/p>\n\n<h2 id=\"on-the-cpu-side\">On the CPU side<\/h2>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">sg_setup()<\/code> call gets a new config item <code class=\"language-plaintext highlighter-rouge\">sg_desc.max_dispatch_calls_per_pass<\/code>\n(default: 1024). This is used to allocate an internal array to keep track of\nwritten storage buffers in a compute pass for hazard tracking purposes.<\/p>\n\n<p>There\u2019s a minor change when creating buffers: It\u2019s now allowed to create\nimmutable buffers without initial content, and such buffers will be\nzero-initialized (note though that dynamic- and streaming-buffers may\nstill have undefined buffer content after creation). Zero-initialization is useful\nwhen using a compute shader to write the initial buffer content instead\nof providing the data from the CPU side during the <code class=\"language-plaintext highlighter-rouge\">sg_make_buffer()<\/code> call.<\/p>\n\n<p>Shaders, pipelines and passes now come in two runtime flavours: \u2018render\u2019 vs \u2018compute\u2019,\nwhere the \u2018render flavours\u2019 are fully compatible with existing code.<\/p>\n\n<p>For shaders, nothing changes either when using sokol-shdc for shader authoring.\nIn that case you just write a compute shader and sokol-shdc will code-generate\na matching <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct which can be plugged directly into the\n<code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> call.<\/p>\n\n<p>A compute pipeline is a regular pipeline object without any render state,\nbut with a compute shader attached:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_pipeline<\/span> <span class=\"n\">pip<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_pipeline<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pipeline_desc<\/span><span class=\"p\">){<\/span>\n  <span class=\"p\">.<\/span><span class=\"n\">compute<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">.<\/span><span class=\"n\">shader<\/span> <span class=\"o\">=<\/span> <span class=\"n\">a_compute_shader<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Finally, kicking off \u2018compute workloads\u2019 happens with a new function <code class=\"language-plaintext highlighter-rouge\">sg_dispatch()<\/code>\ninside \u2018compute passes\u2019:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">compute<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span> <span class=\"p\">});<\/span>\n<span class=\"n\">sg_apply_pipeline<\/span><span class=\"p\">(<\/span><span class=\"n\">pip<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_apply_bindings<\/span><span class=\"p\">(...);<\/span>\n<span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(...);<\/span>\n<span class=\"n\">sg_dispatch<\/span><span class=\"p\">(<\/span><span class=\"n\">x<\/span><span class=\"p\">,<\/span> <span class=\"n\">y<\/span><span class=\"p\">,<\/span> <span class=\"n\">z<\/span><span class=\"p\">);<\/span>\n<span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">sg_dispatch()<\/code> call takes the number of \u2018workgroups\u2019 as arguments\n(same convention as GL, D3D11 and WebGPU, but different from Metal\u2019s <code class=\"language-plaintext highlighter-rouge\">dispatchThreads<\/code> method).<\/p>\n\n<p>Compute- vs render-passes now impose a couple of restrictions (checked\nby the validation layer):<\/p>\n\n<ul>\n  <li>the following functions must only be called in render passes:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">sg_apply_viewport[f]()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">sg_apply_scissor_rect[f]()<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">sg_draw()<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_dispatch()<\/code> must only be called in a compute pass<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> in a compute pass must not attempt to bind vertex- or index-buffers<\/li>\n  <li>the <code class=\"language-plaintext highlighter-rouge\">sg_apply_pipeline()<\/code> pipeline type must match the pass type (e.g. render pipeline\nobjects can only be applied in render passes, and compute pipeline objects only\nin compute passes)<\/li>\n<\/ul>\n\n<h2 id=\"when-not-using-sokol-shdc\">When not using sokol-shdc<\/h2>\n\n<p>If you don\u2019t use sokol-shdc for shader authoring you\u2019ll need to populate the\nall-important <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct passed into <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> yourself\nwith information that matches your shader code:<\/p>\n\n<ul>\n  <li>A nested struct <code class=\"language-plaintext highlighter-rouge\">compute_func<\/code> has been added (similar to existing\n<code class=\"language-plaintext highlighter-rouge\">vertex_func<\/code> and <code class=\"language-plaintext highlighter-rouge\">fragment_func<\/code>) to pass a compute shader function as\nbackend-specific source code or bytecode blob<\/li>\n  <li>A Metal-specific <code class=\"language-plaintext highlighter-rouge\">mtl_threads_per_threadgroup<\/code> nested struct which\ndefines the \u2018workgroup size\u2019 to the Metal API (this is in <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code>\nbecause those values are normally extracted from shader code via reflection)<\/li>\n  <li>The <code class=\"language-plaintext highlighter-rouge\">readonly<\/code> boolean in the storage buffer bindslot declaration is now\nallowed to be false, but only in compute shaders. This flag is\nnow used by sokol-gfx as hint for \u2018resource hazard tracking\u2019 in some backend APIs.<\/li>\n  <li>A new HLSL\/D3D11 specific item <code class=\"language-plaintext highlighter-rouge\">uint8_t register_u_n<\/code> has been added to\nthe nested <code class=\"language-plaintext highlighter-rouge\">storage_buffers[]<\/code> declarations (struct <code class=\"language-plaintext highlighter-rouge\">sg_shader_storage_buffer<\/code>), this is used to communicate the\nHLSL bindslot for writable storage buffer bindings (which are bound as D3D11\n\u2018unordered access views\u2019, while readonly storage buffers continue to be\nbound as \u2018shader resource views\u2019).<\/li>\n<\/ul>\n\n<p>Also please carefully review the backend-specific compute shader samples\nwhich directly pass backend-specific shader code into sokol-gfx:<\/p>\n\n<ul>\n  <li>D3D11: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/d3d11\/instancing-compute-d3d11.c\">instancing-compute-d3d11.c<\/a><\/li>\n  <li>Metal: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/metal\/instancing-compute-metal.c\">instancing-compute-metal.c<\/a><\/li>\n  <li>WebGPU: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/instancing-compute-wgpu.c\">instancing-compute-wgpu.c<\/a><\/li>\n  <li>GL4.3: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/instancing-compute-glfw.c\">instancing-compute-glfw.c<\/a><\/li>\n<\/ul>\n\n<h2 id=\"under-the-hood\">Under the hood<\/h2>\n\n<p>Most of the new code in sokol_gfx.h is just a straight-forward mapping from\nsokol-gfx types and functions into backend 3D-API types and functions.<\/p>\n\n<p>Only two details are worth mentioning:<\/p>\n\n<ul>\n  <li>On Metal, and only on systems without unified memory, GPU-written\nmanaged storage buffers are \u2018synchronized\u2019 at the end of a compute\npass inside <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code>. This synchronization basically updates the\nCPU-side shadow copy of the buffer with the new data that\u2019s been written\nby a compute shader. This requires keeping track of all read\/write storage\nbuffer bindings inside a compute pass (this is what the new <code class=\"language-plaintext highlighter-rouge\">sg_desc.max_dispatch_calls_per_pass<\/code>\nconfig item is used for).<\/li>\n  <li>On GL, <code class=\"language-plaintext highlighter-rouge\">glMemoryBarrier()<\/code> calls are issued (at most once per <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>\ncall) when a storage buffer was previously bound as read\/write (which sets\nan internal \u2018gpu_dirty\u2019 flag).<\/li>\n<\/ul>\n\n<h2 id=\"whats-next\">What\u2019s next<\/h2>\n\n<p>\u2026mainly patching remaining feature gaps in a couple of minor updates:<\/p>\n\n<ul>\n  <li>allow storage buffers to be bound as vertex- and index-buffers<\/li>\n  <li>introducing storage textures which can be written by compute shaders<\/li>\n  <li>more \u2018feature coverage\u2019 by writing a handful more interesting compute samples<\/li>\n<\/ul>\n\n<p>\u2026and what will most likely a bigger update: figure out a proper\nsub-API for <code class=\"language-plaintext highlighter-rouge\">CPU =&gt; GPU<\/code>, <code class=\"language-plaintext highlighter-rouge\">GPU =&gt; CPU<\/code> and <code class=\"language-plaintext highlighter-rouge\">GPU =&gt; GPU<\/code> copies.<\/p>\n","pubDate":"Mon, 03 Mar 2025 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2025\/03\/03\/sokol-gfx-compute-update.html","guid":"https:\/\/floooh.github.io\/2025\/03\/03\/sokol-gfx-compute-update.html"},{"title":"Upcoming Sokol header API changes (Nov 2024)","description":"<p>Update: the \u2018bindings cleanup\u2019 update has been merged on 07-Nov-2024<\/p>\n\n<p>In a couple of days I will merge the next breaking sokol_gfx.h update (aka the\n\u201cBindings Cleanup\u201d). The update also affects sokol-shdc, so if you\u2019re using\nsokol-shdc for shader compilation make sure to update that as well.<\/p>\n\n<ul id=\"markdown-toc\">\n  <li><a href=\"#overview\" id=\"markdown-toc-overview\">Overview<\/a><\/li>\n  <li><a href=\"#updated-documentation-and-example-code\" id=\"markdown-toc-updated-documentation-and-example-code\">Updated documentation and example code<\/a>    <ul>\n      <li><a href=\"#when-using-sokol-shdc\" id=\"markdown-toc-when-using-sokol-shdc\">When using sokol-shdc:<\/a><\/li>\n      <li><a href=\"#when-not-using-sokol-shdc\" id=\"markdown-toc-when-not-using-sokol-shdc\">When <em>not<\/em> using sokol-shdc<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><a href=\"#change-recipes\" id=\"markdown-toc-change-recipes\">Change Recipes<\/a>    <ul>\n      <li><a href=\"#when-using-sokol-shdc-1\" id=\"markdown-toc-when-using-sokol-shdc-1\">When using sokol-shdc:<\/a><\/li>\n      <li><a href=\"#when-not-using-sokol-shdc-1\" id=\"markdown-toc-when-not-using-sokol-shdc-1\">When <em>not<\/em> using sokol-shdc:<\/a><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<h2 id=\"overview\">Overview<\/h2>\n\n<p>In general, the update makes the relationship between the shader resource interface\nand the sokol-gfx resource binding model more explicit, but also more flexible.\nAnother motivation for the change was to prepare the sokol-gfx API for compute\nshader support.<\/p>\n\n<p>The root PR is here: <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/1111\">https:\/\/github.com\/floooh\/sokol\/pull\/1111<\/a>.<\/p>\n\n<p>The TL;DR is:<\/p>\n\n<ul>\n  <li>When using sokol-shdc for shader compilation, the input GLSL source\nnow requires explicit binding annotations via <code class=\"language-plaintext highlighter-rouge\">layout(binding=N)<\/code>, where\n<code class=\"language-plaintext highlighter-rouge\">N<\/code> directly maps to bindslot indices in the sokol-gfx resource binding API.<\/li>\n  <li>The concept of \u2018shader stages\u2019 mostly disappears from the sokol-gfx API,\nshader stages are now only a minor detail of the shader interface reflection\ninformation in the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct passed into the <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code>\nfunction.<\/li>\n  <li>When <em>not<\/em> using sokol-shdc there\u2019s now an explicit mapping from sokol-gfx bindslots\nto 3D backend-specific bindslots. This reduces the sokol-gfx internal\nmagic for mapping the backend-agnostic sokol-gfx binding model to the specific binding\nmodels of the backend 3D APIs (there <em>are<\/em> still some restrictions but only\nwhen they allow a more efficient resource binding implementation in sokol-gfx).<\/li>\n<\/ul>\n\n<p>In general, all changes result in compile errors, and cleaning up the\ncompile errors by following the \u2018change recipes\u2019 below should be enough\nto make your existing code work.<\/p>\n\n<p>The following parts of the public sokol_gfx.h API have changed:<\/p>\n\n<ul>\n  <li>In the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct, the nested vertex- and fragment-stage structs\nfor the image-, sampler- and storage-buffer-bindings have been removed,\nand the bindings arrays have moved up into the root struct.<\/li>\n  <li>In the <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> call, the shader stage parameter has been removed<\/li>\n  <li>The interior of the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct and the typename of nested structs\nhave changed completely (but if you are using sokol-shdc for shader authoring\nyou don\u2019t need to worry about that, since sokol-shdc will code-generate\nthe <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct.<\/li>\n  <li>A number of public API constants have been removed or renamed (but those\nshould rarely show up in user code).<\/li>\n  <li>The enum items in <code class=\"language-plaintext highlighter-rouge\">sg_shader_stage<\/code> have been renamed, and those are now\nonly used in the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct and nowhere else:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_SHADERSTAGE_VS<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">SG_SHADERSTAGE_VERTEX<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SG_SHADERSTAGE_FS<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">SG_SHADERSTAGE_FRAGMENT<\/code><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>The update also has some minor behaviour changes:<\/p>\n\n<ul>\n  <li>Resource bindings can now have gaps, and validation for <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>\nhas been relaxed to allow bindslots in the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct to be occupied\neven when the current shader doesn\u2019t use those bindings. This allows to use\nthe same <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct for different but related shader variants.<\/li>\n  <li>Likewise, uniform block bindslots can now be explicitly defined in the shaders\nwhich allows to \u2018share\u2019 bindslot indices across shaders. Trying to call\n<code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> for a bindslot that isn\u2019t used by the current shader\nis still an error though (not sure yet if this makes sense, could probably\nbe relaxed in a later update)<\/li>\n  <li>There\u2019s now a new (debug-mode only) error check in <code class=\"language-plaintext highlighter-rouge\">sg_draw()<\/code> to make sure\nthat <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> and\/or <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> had been called since the\nlast <code class=\"language-plaintext highlighter-rouge\">sg_apply_pipeline()<\/code> when required.<\/li>\n<\/ul>\n\n<h2 id=\"updated-documentation-and-example-code\">Updated documentation and example code<\/h2>\n\n<blockquote>\n  <p>NOTE: these links will only be uptodate after <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/1111\">PR #1111<\/a> has been merged.<\/p>\n<\/blockquote>\n\n<h3 id=\"when-using-sokol-shdc\">When using sokol-shdc:<\/h3>\n\n<p>Please re-read the sokol-shdc documentation:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-tools\/blob\/master\/docs\/sokol-shdc.md\">https:\/\/github.com\/floooh\/sokol-tools\/blob\/master\/docs\/sokol-shdc.md<\/a><\/p>\n\n<p>Especially the section <code class=\"language-plaintext highlighter-rouge\">Shader Authoring Considerations<\/code>.<\/p>\n\n<p>In the <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/master\/sokol_gfx.h\">sokol_gfx.h header<\/a>, re-read the documentation header above\nthe <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct.<\/p>\n\n<p>Check the updated sokol samples here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/sapp\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/sapp<\/a><\/p>\n\n<h3 id=\"when-not-using-sokol-shdc\">When <em>not<\/em> using sokol-shdc<\/h3>\n\n<p>In the <a href=\"https:\/\/github.com\/floooh\/sokol\/blob\/master\/sokol_gfx.h\">sokol_gfx.h header<\/a>, re-read the updated documentation section <code class=\"language-plaintext highlighter-rouge\">ON SHADER CREATION<\/code>.<\/p>\n\n<p>Next read the updated documentation above the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> structs.<\/p>\n\n<p>Finally check the updated backend-specific samples:<\/p>\n\n<ul>\n  <li>for Metal: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal<\/a><\/li>\n  <li>for D3D11: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11<\/a><\/li>\n  <li>for desktop GL: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw<\/a><\/li>\n  <li>for WebGL\/GLES3: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5<\/a><\/li>\n  <li>for WebGPU: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu<\/a><\/li>\n<\/ul>\n\n<p>Especially note the <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> struct interiors in the <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> calls.<\/p>\n\n<h2 id=\"change-recipes\">Change Recipes<\/h2>\n\n<p>General rule of thumb: fix all places that throw compile errors and\nyou should be good.<\/p>\n\n<h3 id=\"when-using-sokol-shdc-1\">When using sokol-shdc:<\/h3>\n\n<p>First you\u2019ll need to fix your shaders and add explicit binding annotations. When running\nsokol-shdc over your current shader code you\u2019ll get errors looking like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>error: 'binding' : uniform\/buffer blocks require layout(binding=X)\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026or this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>error: 'binding' : sampler\/texture\/image requires layout(binding=X)\n<\/code><\/pre><\/div><\/div>\n\n<p>To fix those errors for the different resource types add <code class=\"language-plaintext highlighter-rouge\">layout(binding=N)<\/code> annotations:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">vs_params<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">sampler<\/span> <span class=\"n\">smp<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"n\">readonly<\/span> <span class=\"n\">buffer<\/span> <span class=\"n\">ssbo<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Note that each resource type (uniform blocks, textures, samplers and storage buffers)\nhas its own bindslot space which is shared across shader stages. Trying to use\nbindslot indices outside those ranges, or using the same bindslot for a resource\ntype in different shader stages will cause a compilation error.<\/p>\n\n<p>The binding ranges per resource type are:<\/p>\n\n<ul>\n  <li>uniform blocks: 0..7<\/li>\n  <li>textures: 0..15<\/li>\n  <li>samplers: 0..15<\/li>\n  <li>storage buffers: 0..7<\/li>\n<\/ul>\n\n<p>\u2026these are also the maximum number of resources of that type that can be bound\non a shader across all shader stages.<\/p>\n\n<p>Next fix the compile errors on the CPU side, you should see errors\nwhen initializing an <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct, when calling <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code>\nand possibly when setting up vertex attributes in the <code class=\"language-plaintext highlighter-rouge\">sg_pipeline_desc<\/code>\nstruct:<\/p>\n\n<ul>\n  <li>in the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct, the nested structs for the vertex\nand fragment shader stage have been removed, and the former per-stage\nbinding arrays have moved up into the root<\/li>\n  <li>in the <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> call, the shader stage argument has been removed<\/li>\n  <li>all code-generated slot constants have new naming schemes (also the vertex\nattribute slot constants)<\/li>\n<\/ul>\n\n<p>For instance if your shader resource interface looks like this:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">vs<\/span>\n<span class=\"c1\">\/\/ a vertex shader uniform block<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">vs_params<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"c1\">\/\/ a vertex shader texture and sampler<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">vs_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">sampler<\/span> <span class=\"n\">vs_smp<\/span><span class=\"p\">;<\/span>\n<span class=\"c1\">\/\/ a vertex shader storage buffer<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"n\">readonly<\/span> <span class=\"n\">buffer<\/span> <span class=\"n\">vs_ssbo<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n\n<span class=\"err\">@<\/span><span class=\"n\">fs<\/span>\n<span class=\"c1\">\/\/ a fragment shader uniform block<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">fs_params<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">};<\/span>\n<span class=\"c1\">\/\/ diffuse, normal and specular textures<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">diffuse_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">2<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">specular_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">texture2D<\/span> <span class=\"n\">normal_tex<\/span><span class=\"p\">;<\/span>\n<span class=\"c1\">\/\/ a common sampler for the above textures<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"k\">uniform<\/span> <span class=\"n\">sampler<\/span> <span class=\"n\">smp<\/span><span class=\"p\">;<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026the matching <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct on the CPU side needs to look like\nthis - note how the array indices match the shader <code class=\"language-plaintext highlighter-rouge\">layout(binding=N)<\/code>:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_bindings<\/span> <span class=\"n\">bnd<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">images<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">diffuse_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">2<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">specular_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">3<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">normal_tex<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_smp<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">smp<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storage_buffers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_ssbo<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and the <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> calls to write the uniform data for the\n<code class=\"language-plaintext highlighter-rouge\">vs_params<\/code> and <code class=\"language-plaintext highlighter-rouge\">fs_params<\/code> uniform blocks now look like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vs_params<\/span><span class=\"p\">));<\/span>\n<span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">fs_params<\/span><span class=\"p\">));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026instead of hardwired numeric indices you can also use code-generated constants\n(note that those have been renamed from a generic <code class=\"language-plaintext highlighter-rouge\">SLOT_*<\/code> to a per-resource-type\nnaming scheme):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_bindings<\/span> <span class=\"n\">bnd<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_buffer<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">images<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">IMG_vs_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">IMG_diffuse_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">diffuse_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">IMG_specular_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">specular_tex<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">IMG_normal_tex<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">normal_tex<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">SMP_vs_smp<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_smp<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">SMP_smp<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">smp<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storage_buffers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"n\">SBUF_vs_ssbo<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vs_ssbo<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026or for the uniform block updates:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(<\/span><span class=\"n\">UB_vs_params<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vs_params<\/span><span class=\"p\">));<\/span>\n<span class=\"n\">sg_apply_uniforms<\/span><span class=\"p\">(<\/span><span class=\"n\">UB_fs_params<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">fs_params<\/span><span class=\"p\">));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026using the code-generated constants has the advantage that changing the\nbindslots in the shader code doesn\u2019t require updating the CPU-side code, but other\nthen that it\u2019s totally fine to use numeric indices.<\/p>\n\n<p>The naming scheme for the code-generated vertex attribute slots has changed\nto use the shader program name for \u2018namespacing\u2019 instead of the vertex shader\nsnippet name.<\/p>\n\n<p>For instance with the following shader fragment:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">vs<\/span> <span class=\"n\">vs<\/span>\n<span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">position<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">color0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">...<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n\n<span class=\"err\">@<\/span><span class=\"n\">fs<\/span> <span class=\"n\">fs<\/span>\n<span class=\"p\">...<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n\n<span class=\"err\">@<\/span><span class=\"n\">program<\/span> <span class=\"n\">cube<\/span> <span class=\"n\">vs<\/span> <span class=\"n\">fs<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The generated vertex attribute slot constants <code class=\"language-plaintext highlighter-rouge\">ATTR_*<\/code> previously looked like this\n(in the sg_pipeline_desc struct):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_pipeline_desc<\/span> <span class=\"n\">desc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">layout<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">attrs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_vs_position<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_vs_color0<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n        <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026now the <code class=\"language-plaintext highlighter-rouge\">ATTR_*<\/code> names look like this (e.g. <code class=\"language-plaintext highlighter-rouge\">ATTR_vs_*<\/code> to <code class=\"language-plaintext highlighter-rouge\">ATTR_cube_*<\/code>):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_pipeline_desc<\/span> <span class=\"n\">desc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">layout<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">attrs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_cube_position<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_cube_color0<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n        <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026it\u2019s also possible to use explicit attribute locations and ignore\nthe code-generated constants, for instance:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"err\">@<\/span><span class=\"n\">vs<\/span> <span class=\"n\">vs<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">location<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span> <span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">position<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">layout<\/span><span class=\"p\">(<\/span><span class=\"n\">location<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">color0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">...<\/span>\n<span class=\"err\">@<\/span><span class=\"n\">end<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_pipeline_desc<\/span> <span class=\"n\">desc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">layout<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">attrs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n            <span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"p\">...,<\/span>\n        <span class=\"p\">},<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026note though that it\u2019s still not allowed to have gaps in the vertex\nattribute slots (this may be supported at a later time).<\/p>\n\n<h3 id=\"when-not-using-sokol-shdc-1\">When <em>not<\/em> using sokol-shdc:<\/h3>\n\n<p>The interior of <code class=\"language-plaintext highlighter-rouge\">sg_shader_desc<\/code> has changed to match the new\n\u2018shader-stage-agnostic\u2019 sokol-gfx binding model. The toplevel-structure\nnow looks like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">sg_shader_desc<\/span> <span class=\"n\">desc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_func<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>         <span class=\"c1\">\/\/ vertex shader source or bytecode<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">fragment_func<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>       <span class=\"c1\">\/\/ fragment shader source or bytecode<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">attrs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>               <span class=\"c1\">\/\/ vertex attribute reflection info<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">uniform_blocks<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>      <span class=\"c1\">\/\/ reflection info for uniform block bindings<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">storage_buffers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>     <span class=\"c1\">\/\/ reflection info for storage buffer bindings<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">images<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>              <span class=\"c1\">\/\/ reflection info for texture bindings<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">samplers<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span>            <span class=\"c1\">\/\/ reflection info for sampler bindings<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">image_sampler_pairs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">...<\/span> <span class=\"p\">},<\/span> <span class=\"c1\">\/\/ how images and samplers are used together in the shader<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The array indices in the <code class=\"language-plaintext highlighter-rouge\">uniform_blocks[]<\/code> array match the <code class=\"language-plaintext highlighter-rouge\">ub_slot<\/code> parameter\nin the <code class=\"language-plaintext highlighter-rouge\">sg_apply_uniforms()<\/code> call:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>sg_shader_desc.uniform_blocks[N] =&gt; sg_apply_uniforms(N, ...)\n<\/code><\/pre><\/div><\/div>\n\n<p>The array indices in the <code class=\"language-plaintext highlighter-rouge\">storage_buffers[]<\/code>, <code class=\"language-plaintext highlighter-rouge\">images[]<\/code> and <code class=\"language-plaintext highlighter-rouge\">samplers[]<\/code> arrays\nmatch the respective indices in the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>sg_shader_desc.images[N] =&gt; sg_bindings.images[N]\nsg_shader_desc.samplers[N] =&gt; sg_bindings.samplers[N]\nsg_shader_desc.storage_buffers[N] =&gt; sg_bindings.storage_buffers[N]\n<\/code><\/pre><\/div><\/div>\n\n<p>Fields that are only required for a specific 3D backend now have consistent\nprefixes:<\/p>\n\n<ul>\n  <li>D3D11\/HLSL: <code class=\"language-plaintext highlighter-rouge\">hlsl_*<\/code><\/li>\n  <li>GL\/GLSL: <code class=\"language-plaintext highlighter-rouge\">glsl_*<\/code><\/li>\n  <li>Metal\/MSL: <code class=\"language-plaintext highlighter-rouge\">msl_*<\/code><\/li>\n  <li>WebGPU\/WGSL: <code class=\"language-plaintext highlighter-rouge\">wgsl_*<\/code><\/li>\n<\/ul>\n\n<p>The resource binding slots now require two new types of information:<\/p>\n\n<ul>\n  <li>the shader stage this resource binding appears on<\/li>\n  <li>a 3D backend specific bindslot<\/li>\n<\/ul>\n\n<p>The backend specific bindslot struct members need to be filled with the\nshader language specific resource bindslot numbers which also need to\nlie within specific ranges:<\/p>\n\n<ul>\n  <li>for uniform block items:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">.hlsl_register_b_n = N;<\/code> &lt;= HLSL <code class=\"language-plaintext highlighter-rouge\">register(bN)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 8)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.msl_buffer_n = N;<\/code> &lt;= &gt;MSL <code class=\"language-plaintext highlighter-rouge\">[[buffer(N)]]<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 8)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.wgsl_group0_binding_n = N;<\/code> &lt;= WGSL <code class=\"language-plaintext highlighter-rouge\">@group(0) @binding(N)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 8)<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>for images:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">.hlsl_register_t_n = N;<\/code> &lt;= HLSL <code class=\"language-plaintext highlighter-rouge\">register(tN)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 24)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.msl_texture_n = N;<\/code> &lt;= MSL <code class=\"language-plaintext highlighter-rouge\">[[texture(N)]]<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 16)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.wgsl_group1_binding_n = N;<\/code> &lt;= WGSL <code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(N)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 128)<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>for samplers:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">.hlsl_register_s_n = N;<\/code> &lt;= HLSL <code class=\"language-plaintext highlighter-rouge\">register(sN)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 16)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.msl_sampler_n = N;<\/code> &lt;= MSL <code class=\"language-plaintext highlighter-rouge\">[[sampler(N)]]<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 16)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.wgsl_group1_binding_n = N;<\/code> &lt;= WGSL <code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(N)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 128)<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li>for storage buffers:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">.hlsl_register_t_n = N;<\/code> &lt;= HLSL <code class=\"language-plaintext highlighter-rouge\">register(tN)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 24)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.msl_register_b_n = N;<\/code> &lt;= MSL <code class=\"language-plaintext highlighter-rouge\">[[buffer(N)]]<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 8) &amp;&amp; (N &lt; 16)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.wgsl_group1_binding_n = N;<\/code> &lt;= WGSL <code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(N)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 128)<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">.glsl_binding_n = N;<\/code> &lt;= GLSL <code class=\"language-plaintext highlighter-rouge\">layout(binding=N)<\/code> where <code class=\"language-plaintext highlighter-rouge\">(N &gt;= 0) &amp;&amp; (N &lt; 16)<\/code><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>These backend-specific bindslots allow a more flexible mapping from the sokol-gfx\nresource binding model to the backend 3D-API binding models, but there are still\nsome restrictions (which typically exist to allow a more efficient resource binding implementation\nin sokol_gfx.h):<\/p>\n\n<ul>\n  <li>in WebGPU\/WGSL, all uniform blocks must be in <code class=\"language-plaintext highlighter-rouge\">@group(0)<\/code> and all other\nresource types in <code class=\"language-plaintext highlighter-rouge\">@group(1)<\/code><\/li>\n  <li>in Metal\/MSL, the <code class=\"language-plaintext highlighter-rouge\">[[buffer(N)]]<\/code> slots 0..7 are reserved for uniform blocks,\nand <code class=\"language-plaintext highlighter-rouge\">[[buffer(N)]]<\/code> slots 8..15 are reserved for storage buffers<\/li>\n<\/ul>\n\n<p>For code examples, check out the backend-specific samples:<\/p>\n\n<ul>\n  <li>for Metal: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal<\/a><\/li>\n  <li>for D3D11: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11<\/a><\/li>\n  <li>for desktop GL: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw<\/a><\/li>\n  <li>for WebGL\/GLES3: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5<\/a><\/li>\n  <li>for WebGPU: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu<\/a><\/li>\n<\/ul>\n\n<p>\u2026and that should be it! Next big thing on the roadmap: compute shader support :)<\/p>\n","pubDate":"Mon, 04 Nov 2024 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2024\/11\/04\/sokol-fall-2024-update.html","guid":"https:\/\/floooh.github.io\/2024\/11\/04\/sokol-fall-2024-update.html"},{"title":"Zig and Emulators","description":"<p>Some quick Zig feedback in the context of a new 8-bit emulator project I started\na little while ago:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/chipz\">https:\/\/github.com\/floooh\/chipz<\/a><\/p>\n\n<p>Currently the project consists of:<\/p>\n\n<ul>\n  <li>a cycle-stepped Z80 CPU emulator (similar to the emulator described\nhere: <a href=\"https:\/\/floooh.github.io\/2021\/12\/17\/cycle-stepped-z80.html\">https:\/\/floooh.github.io\/2021\/12\/17\/cycle-stepped-z80.html<\/a><\/li>\n  <li>chip emulators for Z80 PIO, Z80 CTC and three variants of the AY-3-8910 sound chip<\/li>\n  <li>system emulators for Bombjack, Pengo and Pacman arcade machines,\nand the East German KC85\/2..\/4 home computer series<\/li>\n  <li>a code generation tool to create the Z80 instruction decoder code block<\/li>\n  <li>various tests to check Z80 emulation correctness<\/li>\n<\/ul>\n\n<p>With the exception of an external C dependency for \u2018host system glue\u2019 (the\ncross-platform <a href=\"https:\/\/github.com\/floooh\/sokol-zig\">sokol headers<\/a> used for\nwrapping the platform-specific windowing, input, rendering and audio-output\ncode), the project is around 16 kloc of pure Zig code.<\/p>\n\n<p>I\u2019m not yet sure how this new project will evolve in relation to the <a href=\"https:\/\/github.com\/floooh\/chips\">original C\/C++ \u2018chips\u2019 emulator project<\/a>, but I expect\nthat the Zig project will overtake the C\/C++ project at some point in the future.<\/p>\n\n<h2 id=\"dev-environment\">Dev Environment<\/h2>\n\n<p>I\u2019m coding on an M1 Mac in VSCode with the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ziglang.vscode-zig\">Zig Language Extension<\/a>, and <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vadimcn.vscode-lldb\">CodeLLDB<\/a>\nfor step-debugging.<\/p>\n\n<p>The Zig and ZLS (Zig Language Server) installation is managed with <a href=\"https:\/\/github.com\/tristanisham\/zvm\">ZVM<\/a>.<\/p>\n\n<p>For the most part this setup works pretty well, with a few tweaks:<\/p>\n\n<ul>\n  <li>I\u2019m doing \u2018build-on-save\u2019 to get more complete error information as described here:\n<a href=\"https:\/\/kristoff.it\/blog\/improving-your-zls-experience\/\">Improving Your Zig Language Server Experience<\/a>\n(I\u2019m not bothering with creating separate non-install build targets though)<\/li>\n  <li>With the default Zig VSCode extension settings I was seeing that in long coding\nsession (5..6 hours or so) saving would take longer and longer until it would\neventually get stuck. After asking around on the Zig Discord this could be solved\nby explicitly setting the Zig Language Server as \u2018VSCode Formatting Provider\u2019\nin the Zig Extension settings.<\/li>\n  <li>When debugging, there\u2019s a somewhat annoying issue that the debug line information\nseems to be off in some places, the debugger appears to step into the last\nline of an inactive if-else block for instance. Again, Discord to the rescue,\nthis seems to be a known issue.<\/li>\n<\/ul>\n\n<p>All in all, not yet perfect, but good enough to get shit done.<\/p>\n\n<h2 id=\"zig-comptime-and-generics\">Zig Comptime and Generics<\/h2>\n\n<p>Before diving into language details, I\u2019ll need to provide some minimal\nbackground information of how the chipz emulators work:<\/p>\n\n<p>Microchips of the 70s and 80s were very much like \u2018software libraries, but\nimplemented in hardware\u2019, they followed a minimal standard for interoperability\nso that chips from different manufacturers could be combined into computer\nsystems without requiring too much custom glue logic between them. I think\nit\u2019s fair to say that this \u2018competition through interoperability\u2019 was the main\ndriver for the Cambrian Explosion of cheap 8-bit computer systems in the\n70s and 80s.<\/p>\n\n<p>Microchips communicate with the outside world via input\/output pins, and a\ntypical 8-bit home computer system is essentially just a handful of microchips\ntalking to each other through their \u2018pin API\u2019.<\/p>\n\n<p>The chipz project follows that same idea: The basic building blocks are\nself-contained chip emulators which communicate with other chip emulators via\nvirtual input\/output pins which are mapped to bits in an integer.<\/p>\n\n<p>Chips of that era typically had up to 40 pins which makes them a good\nfit for 64-bit integers used in today\u2019s CPUs.<\/p>\n\n<p>The API of such a chip emulator only has one important function:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">pub<\/span> <span class=\"k\">fn<\/span> <span class=\"n\">tick<\/span><span class=\"p\">(<\/span><span class=\"n\">pins<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u64<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u64<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This tick function executes exactly one clock cycle, it takes an integer\nas input where the bits represent input\/output pins, and returns\nthat same integer with modified bits.<\/p>\n\n<p>Fitting a CPU emulator into such a \u2018cycle-stepped model\u2019 can be a bit of\na challenge and is described in these blog posts (for the 6502 and Z80):<\/p>\n\n<ul>\n  <li>\n    <p><a href=\"https:\/\/floooh.github.io\/2019\/12\/13\/cycle-stepped-6502.html\">A new cycle-stepped 6502 CPU emulator<\/a><\/p>\n  <\/li>\n  <li>\n    <p><a href=\"https:\/\/floooh.github.io\/2021\/12\/17\/cycle-stepped-z80.html\">A new cycle-stepped Z80 emulator<\/a><\/p>\n  <\/li>\n<\/ul>\n\n<p>A whole computer system is then emulated by writing a \u2018system tick function\u2019\nwhich emulates a single clock cycle for the whole system by calling the\ntick functions of each chip emulator and passing pin-state integers\nfrom one chip emulator to the next.<\/p>\n\n<p>There\u2019s two related problems to solve with the above approach:<\/p>\n\n<ul>\n  <li>There\u2019s not enough bits in a 64-bit integer to assign one bit for each\ninter-chip connection of a complete computer system. This means a system\ntick function will need to maintain one pin-state integer for each chip, and\nshuffle bits around before each chip\u2019s tick function is called.<\/li>\n  <li>For direct pin-to-pin connections it makes sense to assign the same bit position\nin different chip emulators to avoid \u2018runtime bit shuffling\u2019 from an output\npin position of one chip to a different input pin position of another chip. Those\ndirect pin-to-pin connections are different in each emulated computer\nsystem, so to make this idea work a specialized chip emulator needs to be\n\u2018stamped out\u2019 for each computer system.<\/li>\n<\/ul>\n\n<p>Both problems can be solved quite elegantly in Zig:<\/p>\n\n<ul>\n  <li>Instead of 64-bit integers for the pin-state we can switch to wide integers\n(u128, u192, u256, \u2026) with enough bits to assign each chip in a system\nits own reserved bit range instead of juggling with multiple 64-bit integers.<\/li>\n  <li>With Zig\u2019s comptime generics it\u2019s possible to stamp out chip emulators\nwhich are specialized by a specific mapping of pins to bit positions in the\nshared wide integer.<\/li>\n<\/ul>\n\n<p>This means a chip emulator is specialized by two comptime configuration values:<\/p>\n\n<ul>\n  <li>a <code class=\"language-plaintext highlighter-rouge\">Bus<\/code> type which is an unsigned integer type with enough bits for all pin-to-pin\nconnections in a system<\/li>\n  <li>a <code class=\"language-plaintext highlighter-rouge\">Pins<\/code> structure which defines a bit position for each input\/output pin\nof a chip emulator<\/li>\n<\/ul>\n\n<p>For the Z80 CPU emulator this pin definition struct looks like this:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">pub<\/span> <span class=\"k\">const<\/span> <span class=\"n\">Pins<\/span> <span class=\"o\">=<\/span> <span class=\"k\">struct<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">DBUS<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"mi\">8<\/span><span class=\"p\">]<\/span><span class=\"nb\">comptime_int<\/span><span class=\"p\">,<\/span>\n    <span class=\"n\">ABUS<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"mi\">16<\/span><span class=\"p\">]<\/span><span class=\"nb\">comptime_int<\/span><span class=\"p\">,<\/span>\n    <span class=\"n\">M1<\/span><span class=\"p\">:<\/span> <span class=\"nb\">comptime_int<\/span><span class=\"p\">,<\/span>\n    <span class=\"n\">MREQ<\/span><span class=\"p\">:<\/span> <span class=\"nb\">comptime_int<\/span><span class=\"p\">,<\/span>\n    <span class=\"n\">IORQ<\/span><span class=\"p\">:<\/span> <span class=\"nb\">comptime_int<\/span><span class=\"p\">,<\/span>\n    <span class=\"c\">\/\/ ...more pins...<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026which is used as nested struct in a <code class=\"language-plaintext highlighter-rouge\">TypeConfig<\/code> struct which holds\nall generic parameters to stamp out a specialized Z80 emulator:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">pub<\/span> <span class=\"k\">const<\/span> <span class=\"n\">TypeConfig<\/span> <span class=\"o\">=<\/span> <span class=\"k\">struct<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">pins<\/span><span class=\"p\">:<\/span> <span class=\"n\">Pins<\/span><span class=\"p\">,<\/span>\n    <span class=\"n\">bus<\/span><span class=\"p\">:<\/span> <span class=\"k\">type<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This <code class=\"language-plaintext highlighter-rouge\">TypeConfig<\/code> struct is used as parameter for a comptime Zig function\nwhich returns a specialized type (this is how Zig does generics):<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">pub<\/span> <span class=\"k\">fn<\/span> <span class=\"n\">Type<\/span><span class=\"p\">(<\/span><span class=\"k\">comptime<\/span> <span class=\"n\">cfg<\/span><span class=\"p\">:<\/span> <span class=\"n\">TypeConfig<\/span><span class=\"p\">)<\/span> <span class=\"k\">type<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"k\">struct<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c\">\/\/ the returned struct is a new type which is comptime-configured<\/span>\n    <span class=\"c\">\/\/ by the 'cfg' type configuration parameter<\/span>\n  <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026now we can stamp out a Z80 CPU emulator that\u2019s specialized for a specific\ncomputer system by the system bus integer type and the Z80 pins mapped to\nspecific bit positions of this integer type:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">z80<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">@import<\/span><span class=\"p\">(<\/span><span class=\"s\">\"z80\"<\/span><span class=\"p\">);<\/span>\n\n<span class=\"k\">const<\/span> <span class=\"n\">Z80<\/span> <span class=\"o\">=<\/span> <span class=\"n\">z80<\/span><span class=\"p\">.<\/span><span class=\"nf\">Type<\/span><span class=\"p\">(<\/span><span class=\"o\">.<\/span><span class=\"p\">{<\/span>\n  <span class=\"p\">.<\/span><span class=\"py\">bus<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">u128<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">.<\/span><span class=\"py\">pins<\/span> <span class=\"o\">=<\/span> <span class=\"o\">.<\/span><span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"py\">DBUS<\/span> <span class=\"o\">=<\/span> <span class=\"o\">.<\/span><span class=\"p\">{<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span> <span class=\"mi\">2<\/span><span class=\"p\">,<\/span> <span class=\"mi\">3<\/span><span class=\"p\">,<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span> <span class=\"mi\">5<\/span><span class=\"p\">,<\/span> <span class=\"mi\">6<\/span><span class=\"p\">,<\/span> <span class=\"mi\">7<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"py\">ABUS<\/span> <span class=\"o\">=<\/span> <span class=\"o\">.<\/span><span class=\"p\">{<\/span> <span class=\"mi\">8<\/span><span class=\"p\">,<\/span> <span class=\"mi\">9<\/span><span class=\"p\">,<\/span>  <span class=\"c\">\/\/ ... },<\/span>\n    <span class=\"c\">\/\/ ...<\/span>\n  <span class=\"p\">}<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This specific <code class=\"language-plaintext highlighter-rouge\">Z80<\/code> type uses a 128-bit pin-state integer and maps its own\npins to bit positions starting at bit 0, with the first 8 bits being the\ndata bus (most other chips in any computer system will also map their\ndata bus pins to the same bit range, since the data bus is usually shared\nbetween all chips in a system).<\/p>\n\n<p>Note that <code class=\"language-plaintext highlighter-rouge\">Z80<\/code> is just a type, not a runtime object. To get a default-initialized\nZ80 CPU object:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">var<\/span> <span class=\"n\">cpu<\/span> <span class=\"o\">=<\/span> <span class=\"n\">Z80<\/span><span class=\"p\">{};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This example doesn\u2019t look like much, it\u2019s \u201cjust Zig code\u201d after all, but this\nis exactly what makes generic programming in Zig so elegant and powerful.<\/p>\n\n<p>Arbitrarily complex comptime config options can be \u2018baked\u2019 into types,\nand dynamic runtime configuration options can be passed in a \u2018construction\u2019 function\non that type, and all is just regular Zig code from top to bottom:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">var<\/span> <span class=\"n\">obj<\/span> <span class=\"o\">=<\/span> <span class=\"n\">Type<\/span><span class=\"p\">(<\/span><span class=\"o\">.<\/span><span class=\"p\">{<\/span>\n  <span class=\"c\">\/\/ comptime options...<\/span>\n  <span class=\"p\">.<\/span><span class=\"py\">bus<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">u128<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">.<\/span><span class=\"py\">pins<\/span> <span class=\"o\">=<\/span> <span class=\"o\">.<\/span><span class=\"p\">{<\/span> <span class=\"o\">...<\/span> <span class=\"p\">},<\/span>\n<span class=\"p\">}).<\/span><span class=\"nf\">init<\/span><span class=\"p\">(<\/span><span class=\"o\">.<\/span><span class=\"p\">{<\/span>\n  <span class=\"c\">\/\/ additional runtime options...<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and this is just scratching the surface. There\u2019s a couple of really\ninteresting side effects of this 2-step approach (first build the type,\nthen build an object from that type):<\/p>\n\n<ul>\n  <li>Can use designated-init-syntax for configuring the type which is just *chef\u2019s kiss*\nbecause it makes the code very readable (no guessing what a generic parameter\nactually does because the name is right there in the code).<\/li>\n  <li>TypeConfig structs can be composed by nesting other TypeConfig structs,\nor generic parameters in general, which then can be used to build\ntypes inside types (Yo Dawg\u2026).<\/li>\n  <li>It\u2019s possible to build different struct interiors based on comptime\nparameters (for instance the different KC85 models have different\nruntime-config struct interiors for configuring model-specific features,\nwhich makes \u2018accidential misconfiguration\u2019 an immediate compile error).<\/li>\n<\/ul>\n\n<p>In conclusion, the idea to use Zig\u2019s comptime features to stamp out specialized\nper-system chip and system emulators works exceptionally well and is (IMHO)\n<em>much<\/em> more enjoyable than C++ or Rust generic programming (I\u2019m sure C++ and\nRust can do the same things with sufficient template magic, but this code\ndefinitely won\u2019t look as straightforward as the Zig version).<\/p>\n\n<h2 id=\"bit-twiddling-and-integer-math-can-be-awkward\">Bit Twiddling and Integer Math can be awkward<\/h2>\n\n<p>This section is hard to write because it\u2019s criticizing without offering an\nobviously better solution, please read it as \u2018constructive criticism\u2019. Hopefully Zig will\nbe able to fix some of those things on the road towards 1.0.<\/p>\n\n<p>Zig\u2019s integer handling is quite different from C:<\/p>\n\n<ul>\n  <li>arbitrary bit-width integers are the norm, not the exception<\/li>\n  <li>there is no concept of integer promotion in math expressions\n(not that I noticed at least)<\/li>\n  <li>implicit conversion between different integer types is only\nallowed when no data loss can happen (e.g. an u8 can be assigned to an\nu16, but assigning an u16 to an u8 requires an explicit cast)<\/li>\n  <li>mixing signed and unsigned values in expressions isn\u2019t allowed<\/li>\n  <li>overflow is checked in Debug and ReleaseSafe mode, and there are separate\noperators for \u2018intended wraparound\u2019<\/li>\n<\/ul>\n\n<p>At first glance these features look pretty nice because they fix some obvious\nfootguns in C and C++. Arbitrary width integer types are especially useful for\nemulator code, because hardware chips are full of \u2018odd-width\u2019 counters and\nregisters (3, 5, 20 bits etc\u2026). Directly mapping such registers to types like\nu3, u5 or u20 should potentially allow for more readable and \u2018expressive\u2019 code.<\/p>\n\n<p>Unfortunately, in reality it\u2019s not so clear cut. While C is definitely too\nsloppy when it comes to integer math, Zig might swing the pendulum a bit too\nfar into the other direction by requiring too much explicit casting.<\/p>\n\n<p>The most extreme example I stumbled over was implementing the Z80\u2019s indexed\naddressing mode (e.g. those instructions involving <code class=\"language-plaintext highlighter-rouge\">(IX+d)<\/code> or <code class=\"language-plaintext highlighter-rouge\">(IY+d)<\/code>. This\ntakes the byte <code class=\"language-plaintext highlighter-rouge\">d<\/code> and adds it as a signed quantity and with wraparound to a 16\nbit address (e.g. the byte is sign-extended to a 16-bit value before the\naddition).<\/p>\n\n<p>In C this is quite straightforward:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">uint16_t<\/span> <span class=\"nf\">addi8<\/span><span class=\"p\">(<\/span><span class=\"kt\">uint16_t<\/span> <span class=\"n\">addr<\/span><span class=\"p\">,<\/span> <span class=\"kt\">uint8_t<\/span> <span class=\"n\">offset<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">addr<\/span> <span class=\"o\">+<\/span> <span class=\"p\">(<\/span><span class=\"kt\">int8_t<\/span><span class=\"p\">)<\/span><span class=\"n\">offset<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The simplest way I could come up with to do the same in Zig is:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">addi8<\/span><span class=\"p\">(<\/span><span class=\"n\">addr<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u16<\/span><span class=\"p\">,<\/span> <span class=\"n\">offset<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u16<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">addr<\/span> <span class=\"o\">+%<\/span> <span class=\"nb\">@as<\/span><span class=\"p\">(<\/span><span class=\"kt\">u16<\/span><span class=\"p\">,<\/span> <span class=\"nb\">@bitCast<\/span><span class=\"p\">(<\/span><span class=\"nb\">@as<\/span><span class=\"p\">(<\/span><span class=\"kt\">i16<\/span><span class=\"p\">,<\/span> <span class=\"nb\">@as<\/span><span class=\"p\">(<\/span><span class=\"kt\">i8<\/span><span class=\"p\">,<\/span> <span class=\"nb\">@bitCast<\/span><span class=\"p\">(<\/span><span class=\"n\">offset<\/span><span class=\"p\">)))));<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Note how the integer conversion gets totally drowned in \u2018@-litter\u2019.<\/p>\n\n<p>Both functions result in the same x86 and ARM assembly output (with -O3 for C\nand any of the Release modes in Zig):<\/p>\n\n<pre><code class=\"language-assembly\">addi8:\n  movsx eax, sil  ; move low byte of esi into eax with sign-extension\n  add eax, edi    ; eax += edi\n  ret\n<\/code><\/pre>\n\n<p>For ARM (looks like ARM handles the sign-extension right in the add instruction, not very RISC-y but neat!):<\/p>\n\n<pre><code class=\"language-assembly\">addi8:\n  add w0, w0, w1, sxtb\n  ret\n<\/code><\/pre>\n\n<p>IMHO when the assembly output of a compiler looks so much more straightforward\nthan the high level compiler input, it becomes a bit hard to justify why\nhigh level programming languages had been invented in the first place ;)<\/p>\n\n<p>Apart from that extreme case (which only exists once in the whole code\nbase), narrowing conversions are much more common when writing code that\nmixes different integer widths, and those narrowing conversions require\nexplicit casts, and those explicit casts may reduce readability quite\na bit.<\/p>\n\n<p>The basic idea to only allow implicit conversions that can\u2019t lose data\nis definitely a good one, but very often a cast is required even though the\ncompiler has all the information it needs at compile time to prove that no\ninformation is lost.<\/p>\n\n<p>For instance this Zig code currently is an error:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">trunc4<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u4<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">val<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mi\">0xF<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The expression result would fit into an u4, yet an <code class=\"language-plaintext highlighter-rouge\">@intCast<\/code> or\n<code class=\"language-plaintext highlighter-rouge\">@truncate<\/code> is required to make it work:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">trunc4<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u4<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">@intCast<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mi\">0xF<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Similar situation with a right-shift:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">broken<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u4<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">val<\/span> <span class=\"o\">&gt;&gt;<\/span> <span class=\"mi\">4<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">fn<\/span> <span class=\"n\">works<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u4<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">@truncate<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span> <span class=\"o\">&gt;&gt;<\/span> <span class=\"mi\">4<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Somewhat surprisingly, this works fine though:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">const<\/span> <span class=\"n\">a<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0xFF<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">const<\/span> <span class=\"n\">b<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u4<\/span> <span class=\"o\">=<\/span> <span class=\"n\">a<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mi\">0xF<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">const<\/span> <span class=\"n\">c<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u4<\/span> <span class=\"o\">=<\/span> <span class=\"n\">a<\/span> <span class=\"o\">&gt;&gt;<\/span> <span class=\"mi\">4<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>A similar problem exists with loop variables, which are always of type usize and\nwhich need to be explicitly narrowed even if the loop count is guaranteed to\nfit into a smaller type:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"mi\">0<\/span><span class=\"o\">..<\/span><span class=\"mi\">16<\/span><span class=\"p\">)<\/span> <span class=\"p\">|<\/span><span class=\"mi\">_<\/span><span class=\"n\">i<\/span><span class=\"p\">|<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">const<\/span> <span class=\"n\">i<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u4<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">@intCast<\/span><span class=\"p\">(<\/span><span class=\"mi\">_<\/span><span class=\"n\">i<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>There\u2019s also surprising cases like this:<\/p>\n\n<p>Assuming that:<\/p>\n\n<ul>\n  <li>a: u16 = 0xF000<\/li>\n  <li>b: u16 = 0x1000<\/li>\n  <li>c: u32 = 0x10000<\/li>\n<\/ul>\n\n<p>This expression creates an overflow error:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">const<\/span> <span class=\"n\">d<\/span> <span class=\"o\">=<\/span> <span class=\"n\">a<\/span> <span class=\"o\">+<\/span> <span class=\"n\">b<\/span> <span class=\"o\">+<\/span> <span class=\"n\">c<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026but this doesn\u2019t:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">const<\/span> <span class=\"n\">e<\/span> <span class=\"o\">=<\/span> <span class=\"n\">c<\/span> <span class=\"o\">+<\/span> <span class=\"n\">a<\/span> <span class=\"o\">+<\/span> <span class=\"n\">b<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The type of <code class=\"language-plaintext highlighter-rouge\">d<\/code> and <code class=\"language-plaintext highlighter-rouge\">e<\/code> is both <code class=\"language-plaintext highlighter-rouge\">u32<\/code> btw (which I find also a bit surprising,\nit means that Zig already picks the widest input type as the result type, but\nit doesn\u2019t promote the other inputs to this widest type).<\/p>\n\n<p>And here\u2019s another surprising behaviour I stumbled over:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c\">\/\/ self.sprite_coords[] is an array of bytes<\/span>\n<span class=\"k\">const<\/span> <span class=\"n\">px<\/span><span class=\"p\">:<\/span> <span class=\"kt\">usize<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">272<\/span> <span class=\"o\">-<\/span> <span class=\"n\">self<\/span><span class=\"p\">.<\/span><span class=\"py\">sprite_coords<\/span><span class=\"p\">[<\/span><span class=\"n\">sprite_index<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">1<\/span><span class=\"p\">];<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This produces the error <code class=\"language-plaintext highlighter-rouge\">error: type 'u8' cannot represent integer value '272'<\/code>.\nWhy Zig tries to fit the constant 272 into an u8 instead of picking a wider type\nis a bit of a mystery tbh.<\/p>\n\n<p>One solution is to widen the value read from the array:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">px<\/span><span class=\"p\">:<\/span> <span class=\"kt\">usize<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">272<\/span> <span class=\"o\">-<\/span> <span class=\"nb\">@as<\/span><span class=\"p\">(<\/span><span class=\"kt\">usize<\/span><span class=\"p\">,<\/span> <span class=\"n\">self<\/span><span class=\"p\">.<\/span><span class=\"py\">sprite_coords<\/span><span class=\"p\">[<\/span><span class=\"n\">sprite_index<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">1<\/span><span class=\"p\">]);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>But this works too:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">const<\/span> <span class=\"n\">px<\/span><span class=\"p\">:<\/span> <span class=\"kt\">usize<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">@as<\/span><span class=\"p\">(<\/span><span class=\"kt\">u9<\/span><span class=\"p\">,<\/span> <span class=\"mi\">272<\/span><span class=\"p\">)<\/span> <span class=\"o\">-<\/span> <span class=\"n\">self<\/span><span class=\"p\">.<\/span><span class=\"py\">sprite_coords<\/span><span class=\"p\">[<\/span><span class=\"n\">sprite_index<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">1<\/span><span class=\"p\">];<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In conclusion, I only understood that C\u2019s integer promotion actually has an\nimportant purpose after missing it so badly in Zig :D<\/p>\n\n<p>I think C\u2019s main problem with integer promotion is that it promotes to <code class=\"language-plaintext highlighter-rouge\">int<\/code>,\nand int being stuck at 32-bits even on 64-bit CPUs (not moving the <code class=\"language-plaintext highlighter-rouge\">int<\/code> type\nto 64 bits during the transition from 32- to 64-bit CPUs was a pretty stupid\ndecision in hindsight).<\/p>\n\n<p>TBF though, just extending to the natural word size (e.g. 64 bits) wouldn\u2019t\nhelp much in Zig when using wide integers like u128.<\/p>\n\n<p>In any case, I hope that the current status quo isn\u2019t what ends up in Zig 1.0\nand that a way can be found to reduce \u2018@-litter\u2019 in mixed-width integer expressions\nwithout going back entirely to C\u2019s admittedly too sloppy integer promotion and\nimplicit conversion rules.<\/p>\n\n<p>Asking around on the Zig Discord there seems to be a proposal which lets\noperators narrow the result type for comptime known values (which if I understand\nit right would make the result type of the  expression <code class=\"language-plaintext highlighter-rouge\">a &amp; 0xF<\/code> a <code class=\"language-plaintext highlighter-rouge\">u4<\/code> instead of\nwhatever wider type <code class=\"language-plaintext highlighter-rouge\">a<\/code> is).<\/p>\n\n<p>Another idea that might make sense is to promote integers to the widest\ninput type. Currently the compiler already seems to use the widest\ninput type in an expression as result type, promoting the other\ninputs to this widest type looks like a logical step to me.<\/p>\n\n<p>I would keep the strict separation of signed and unsigned integer types\nthough, e.g. mixed-sign expressions are not allowed, and any theoretical\ninteger promotion should never happen \u2018across signedness\u2019.<\/p>\n\n<p>From my own experience in C (where I don\u2019t allow implicit sign-conversion\nvia -Wsign-conversion warnings) I can tell that this will feel painful\nin the beginning for C and C++ coders, but it makes for better code and API\ndesign in the long run.<\/p>\n\n<p>This experience (of transitioning to more restrictive but also more correct C\ncode by enabling certain warnings) is also why I\u2019m giving Zig some slack about\nits integer conversion strictness. After all, maybe I\u2019m just not used to it\nyet. But OTH, I have by now written enough Zig code that I should slowly get\nused to it, but it <em>still<\/em> feels bumpy. All in all I think this is an area\nwhere \u2018strict design purity\u2019 can harm the language in the long run though, and\na better balance should be found between strictness, coding convenience and\nreadability.<\/p>\n\n<h2 id=\"using-wide-integers-with-bit-twiddling-code-is-fast\">Using wide integers with bit twiddling code is fast<\/h2>\n\n<p>Using a 128 bit integer variable for the emulator system bus works\nnicely and doesn\u2019t have a relevant performance impact. In fact, with a bit of\ncare (by not using bit twiddling operations that cross a 64-bit boundary) the\nproduced assembly code is identical to doing the same operation on a simple\n64-bit variable.<\/p>\n\n<p>For instance extracting an 8-bit value from the upper half of an 128-bit integer:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">getu8<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u128<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">@truncate<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span> <span class=\"o\">&gt;&gt;<\/span> <span class=\"mi\">64<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026is just moving the register which holds the upper 64 bits into the return\nvalue register:<\/p>\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>getu8:\n  mov rax, rsi\n  ret\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026which is the same cost as extracting an 8-bit value from a 64-bit variable:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">getu8<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u64<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">@truncate<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>getu8:\n  mov rax, rdi\n  ret\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026just make sure that the operation doesn\u2019t cross 64-bit boundaries:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">fn<\/span> <span class=\"n\">getu8<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u128<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">@truncate<\/span><span class=\"p\">(<\/span><span class=\"n\">val<\/span> <span class=\"o\">&gt;&gt;<\/span> <span class=\"mi\">60<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026because this now involves actual bit twiddling:<\/p>\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>getu8:\n  shl esi, 4\n  shr rdi, 60\n  lea eax, [rdi + rsi]\n  ret\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"debug-performance\">Debug Performance<\/h2>\n\n<p>Release performance of my C emulator code (with -O3) and my Zig code (with\n-ReleaseFast) is roughly in the same ballpark, but I\u2019m seeing a pretty big\ndifference in Debug performance:<\/p>\n\n<ul>\n  <li>in C, debug performance is roughly 2x slower than -O3<\/li>\n  <li>in Zig, debug performance is roughly 3..4x slower than ReleaseFast<\/li>\n<\/ul>\n\n<p>I haven\u2019t figured out why yet, but it\u2019s not the most obvious candidate (range and\noverflow checks) since ReleaseSafe performance is nearly identical with ReleaseFast\n(interestingly ReleaseSmall is the slowest Release build config, it\u2019s about 40% slower\nthan both ReleaseFast and ReleaseSafe).<\/p>\n\n<p>One important difference between my C and Zig code is that in C I\u2019m using tons\nof small preprocessor macros to make bit twiddling expressions more readable.\nIn Zig these are replaced with inline functions (<code class=\"language-plaintext highlighter-rouge\">inline<\/code> in Zig isn\u2019t just an\noptimization hint, it causes the function body to be inlined also in debug\nmode).<\/p>\n\n<p>At first glance Zig\u2019s inline functions seem to be a good replacement for\nC preprocessor macros, but when looking at the generated code in debug mode,\nthe compiler still pushes and pops function arguments through the stack even though\nthe function body is inlined.<\/p>\n\n<p>Consider this Zig code:<\/p>\n\n<div class=\"language-zig highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">inline<\/span> <span class=\"k\">fn<\/span> <span class=\"n\">add<\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">,<\/span> <span class=\"n\">b<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"n\">a<\/span> <span class=\"o\">+%<\/span> <span class=\"n\">b<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">fn<\/span> <span class=\"n\">add_1<\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">,<\/span> <span class=\"n\">b<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"n\">add<\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span><span class=\"p\">,<\/span> <span class=\"n\">b<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">fn<\/span> <span class=\"n\">add_2<\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">,<\/span> <span class=\"n\">b<\/span><span class=\"p\">:<\/span> <span class=\"kt\">u8<\/span><span class=\"p\">)<\/span> <span class=\"kt\">u8<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"n\">a<\/span> <span class=\"o\">+%<\/span> <span class=\"n\">b<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026in release mode, both functions produce the same code as expected:<\/p>\n\n<pre><code class=\"language-assembly\">add_1:\n  lea eax, [rsi + rdi]\n  ret\n\nadd_2:\n  lea eax, [rsi + rdi]\n  ret\n<\/code><\/pre>\n\n<p>But in debug mode, the function which calls the inline function has a slightly\nhigher overhead because of additional stack traffic:<\/p>\n\n<pre><code class=\"language-assembly\">add_1:\n  push rbp\n  mov rbp, rsp\n  sub rsp, 5\n  mov cl, sil\n  mov al, dil\n  mov byte ptr [rbp - 4], al\n  mov byte ptr [rbp - 3], cl\n  mov byte ptr [rbp - 2], al\n  mov byte ptr [rbp - 1], cl\n  add al, cl\n  mov byte ptr [rbp - 5], al\n  mov al, byte ptr [rbp - 5]\n  movzx eax, al\n  add rsp, 5\n  pop rbp\n  ret\n\nadd_2:\n  push rbp\n  mov rbp, rsp\n  sub rsp, 2\n  mov cl, sil\n  mov al, dil\n  mov byte ptr [rbp - 2], al\n  mov byte ptr [rbp - 1], cl\n  add al, cl\n  movzx eax, al\n  add rsp, 2\n  pop rbp\n  ret\n<\/code><\/pre>\n\n<p>TBH though it\u2019s unlikely that inline function overhead is the only contributor to the\nslower debug performance, but it could be many such small papercuts combined.<\/p>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n<p>I enjoy working with Zig immensely despite the few warts I encountered, for the\nmost part the code just \u2018flows out of the hand\u2019 which IMHO is an\nimportant property of a programming language. It\u2019s encouraging to see how areas\nwhich were a bumpy ride during the 0.10 to 0.11 versions have improved and\nstabilized (most importantly the build and package management system).<\/p>\n\n<p>It\u2019s also interesting how the \u2018most popular design fault\u2019 that comes up in every\nsingle Zig discussion (currently that\u2019s \u2018unused variables are errors\u2019) is a\ncomplete non-issue (for me at least, not once in that 16-kloc project was that\nan annoyance), while the issue that actually mildly annoyed me in real world\ncode (the <code class=\"language-plaintext highlighter-rouge\">@-litter<\/code> in mixed-width integer expressions) is still very much\nunder the radar. Maybe also because mixed-width and bit twiddling code might\nnot be all that common in typical Zig projects, most integer code is probably\nabout computing array indices or data offsets and happen in usize.<\/p>\n\n<p>I also completely left out a whole chapter about code generation with Zig\n(which would have been mostly about string processing and memory management), simply\nbecause the blog post would have become too big, and it is probably an\ninteresting enough topic for its own blog post. This is also an area where Zig is\ndifferent enough from C, mid-level languages like C++ or Rust, and high\nlevel memory-managed languages that I don\u2019t feel quite confident enough yet to\nhave found the right solution to questions like \u2018who owns the underlying\nmemory of a slice returned from a function\u2019 - I have solutions of course,\nbut I\u2019m not entirely happy with them because it feels like a throwback\nto my first forays into C and C++.<\/p>\n\n<p>In short, I don\u2019t want to burden myself (too much) with memory ownership questions, even\nin low level systems programming languages. Typically in C I avoid such\nproblems with a \u2018mostly value-driven approach\u2019 instead of returning references\nto data, I return a copy of the data (unless of course it\u2019s about bulk data\nlike images, 3d meshes, file content etc.. but those are special cases which\nare easy to deal with using manual memory management).<\/p>\n\n<p>Zig is leaning in heavily on slices though, which are just pointer\/size pairs\nwithout any concept of ownership. It would be nice if Zig had some syntax sugar\nto make working with arrays just as flexible as with slices, because arrays are\nvalue types and avoid all the ownership footguns of slices. I think mostly this\ncomes down to implementing a handful \u2018missing features\u2019 from C99 designated\ninitialization (like <a href=\"https:\/\/github.com\/ziglang\/zig\/issues\/6068\">#6068<\/a>) or maybe even\nlooking at languages like JS and TS (\u2026shock and gasps from the audience!!! I\nknow but bear with me) for a couple of features which make working with struct\nand array values more convenient (like destructuring and spreading).<\/p>\n\n<p>\u2026but I\u2019m already halfway into that other blog post which I wanted to\navoid, so let\u2019s end it here lol.<\/p>\n","pubDate":"Sat, 24 Aug 2024 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2024\/08\/24\/zig-and-emulators.html","guid":"https:\/\/floooh.github.io\/2024\/08\/24\/zig-and-emulators.html"},{"title":"Upcoming Sokol header API changes (May 2024)","description":"<p>Aka: \u201cthe storage buffer update\u201d<\/p>\n\n<p>In a couple of days I will merge the next sokol-gfx feature update which adds\ninitial storage buffer support. The update also affects other headers and tools\n(most notably sokol_app.h, all headers with embedded shaders, and sokol-shdc -\nthe cross-backend shader compiler).<\/p>\n\n<p>The bad news first:<\/p>\n\n<ul>\n  <li>This is \u2018gpu-readonly\u2019 support, e.g. it\u2019s not possible (yet) to write to storage buffers\nfrom shader code, gpu-write support will come in a future \u2018compute shaders\u2019 update.<\/li>\n  <li>The following platform\/backend combos don\u2019t get storage buffer support:\n    <ul>\n      <li>all GLES3 backends (WebGL2, iOS+GLES3, Android): for WebGL2 and iOS there is no\nother choice since they are stuck with GLES 3.0, for Android, storage buffer\nsupport may be added later<\/li>\n      <li>macOS+GL: macOS is stuck at GL 4.1, while storage buffers require at least\nGL 4.3<\/li>\n    <\/ul>\n  <\/li>\n  <li>This leaves the following platform\/backend combos which support storage buffers:\n    <ul>\n      <li>macOS + Metal<\/li>\n      <li>iOS + Metal<\/li>\n      <li>Windows + D3D11<\/li>\n      <li>Windows + GL<\/li>\n      <li>Linux + GL<\/li>\n      <li>Web + WebGPU<\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>Storage buffers provide a convenient way to communicate\nlarge array-like data to shaders (the minimum guaranteed size for storage buffers\nis 128 MBytes), for instance:<\/p>\n\n<ul>\n  <li>for \u2018vertex pulling\u2019 to load per-vertex and\/or per-instance data from\nstorage buffers instead of relying on the fixed function vertex input\nstage<\/li>\n  <li>as a more convenient and flexible way to load random access\ndata in shaders compared to the old-school way of using\n\u2018data textures\u2019.<\/li>\n<\/ul>\n\n<p>\u2026and as a \u2018drive-by\u2019 feature: sokol-gfx now finally allows to kick off\na draw call without any resource bindings and instead synthesize vertices\n\u2018out of thin air\u2019 in the vertex shader.<\/p>\n\n<p>The root PR for the update is here: <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/1007\">#1007<\/a>.<\/p>\n\n<h2 id=\"new-sample-code\">New sample code<\/h2>\n\n<p>The following backend-agnostic samples have been added (those use sokol_app.h and sokol-shdc).<\/p>\n\n<blockquote>\n  <p>NOTE: You\u2019ll need a recent Chrome for the WebGPU sample links to work, also expect\nsome general breakage and rendering artifacts depending on the platform (for\ninstance Chrome on Android straight up crashes the tab\non most samples). Also please note that the source code links in those samples\nwill not be valid until all the update PRs have been merged.<\/p>\n<\/blockquote>\n\n<ul>\n  <li><strong>triangle-bufferless-sapp<\/strong>: this demonstrates rendering without buffers (and\nis the only new sample that also works on backends without storage buffer support):\n    <ul>\n      <li>WebGPU: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/triangle-bufferless-sapp.html\">triangle-bufferless-sapp.html<\/a><\/li>\n      <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/triangle-bufferless-sapp.c\">sapp\/triangle-bufferless-sapp.c<\/a><\/li>\n      <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/triangle-bufferless-sapp.glsl\">sapp\/triangle-bufferless-sapp.glsl<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><strong>vertexpull-sapp<\/strong>: the cube-sapp sample ported to vertex pulling:\n    <ul>\n      <li>WebGPU: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/vertexpull-sapp.html\">vertexpull-sapp.html<\/a><\/li>\n      <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexpull-sapp.c\">sapp\/vertexpull-sapp.c<\/a><\/li>\n      <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/vertexpull-sapp.glsl\">sapp\/vertexpull-sapp.glsl<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><strong>sbuftex-sapp<\/strong>: a sample which uses a storage buffer in the fragment shader stage:\n    <ul>\n      <li>WebGPU: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/sbuftex-sapp.html\">sbuftex-sapp.html<\/a><\/li>\n      <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbuftex-sapp.c\">sapp\/sbuftex-sapp.c<\/a><\/li>\n      <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/sbuftex-sapp.glsl\">sapp\/sbuftex-sapp.glsl<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><strong>instancing-pull-sapp<\/strong>: vertex pulling and instancing via storage buffers:\n    <ul>\n      <li>WebGPU: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/instancing-pull-sapp.html\">instancing-pull-sapp.html<\/a><\/li>\n      <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-pull-sapp.c\">sapp\/instancing-pull-sapp.c<\/a><\/li>\n      <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/instancing-pull-sapp.glsl\">sapp\/instancing-pull-sapp.glsl<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><strong>ozz-storagebuffer-sapp<\/strong>: the ozz-skin sample rewritten to pull vertices, instance- and skinning-matrices from storage buffers:\n    <ul>\n      <li>WebGPU: <a href=\"https:\/\/floooh.github.io\/sokol-webgpu\/ozz-storagebuffer-sapp.html\">ozz-storagebuffer-sapp.html<\/a><\/li>\n      <li>C code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/ozz-storagebuffer-sapp.cc\">sapp\/ozz-storagebuffer-sapp.cc<\/a><\/li>\n      <li>GLSL code: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/ozz-storagebuffer-sapp.glsl\">sapp\/ozz-storagebuffer-sapp.glsl<\/a><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>The following backend-specific samples demonstrate how to use storage buffers without the sokol-shdc shader compiler:<\/p>\n\n<ul>\n  <li><strong>D3D11<\/strong> <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/d3d11\/vertexpulling-d3d11.c\">d3d11\/vertexpulling-d3d11.c<\/a><\/li>\n  <li><strong>Metal<\/strong>: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/metal\/vertexpulling-metal.c\">metal\/vertexpulling-metal.c<\/a><\/li>\n  <li><strong>WebGPU<\/strong>: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/vertexpulling-wgpu.c\">wgpu\/vertexpulling-wgpu.c<\/a><\/li>\n  <li><strong>desktop GL<\/strong>: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/vertexpulling-glfw.c\">glfw\/vertexpulling-glfw.c<\/a><\/li>\n<\/ul>\n\n<h2 id=\"how-to-check-for-storage-buffer-support\">How to check for storage buffer support<\/h2>\n\n<p>To check for storage buffer support at runtime, call <code class=\"language-plaintext highlighter-rouge\">sg_query_features()<\/code> and check the <code class=\"language-plaintext highlighter-rouge\">storage_buffer<\/code> boolean in the result:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_query_features<\/span><span class=\"p\">().<\/span><span class=\"n\">storage_buffer<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ storage buffers are supported...<\/span>\n<span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ storage buffers are *NOT* supported...<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"desktop-gl-version-caveats-and-a-minor-breaking-change\">Desktop GL version caveats (and a minor breaking change)<\/h2>\n\n<p>The sokol_gfx.h desktop-GL backend will now query what GL version it runs on\nto decide whether storage buffers are supported (storage buffers were added in GL 4.3).<\/p>\n\n<p>The expected minimal version has been bumped to 4.1 on macOS and 4.3 on other platforms, this\nalso means that sokol_app.h will now by default create a 4.1 context on macOS, and 4.3 context\non other platforms.<\/p>\n\n<p>Since the GL version is now flexible, the configuration define <code class=\"language-plaintext highlighter-rouge\">SOKOL_GLCORE33<\/code> doesn\u2019t make\nmuch sense anymore and has been renamed to <code class=\"language-plaintext highlighter-rouge\">SOKOL_GLCORE<\/code>. You\u2019ll get a proper compile\nerror when trying to build with the old <code class=\"language-plaintext highlighter-rouge\">SOKOL_GLCORE33<\/code> define.<\/p>\n\n<p>Apart from rebuilding your shaders via an updated sokol-shdc, this is the only required\nchange for existing code.<\/p>\n\n<p>In sokol-shdc, the target language <code class=\"language-plaintext highlighter-rouge\">glsl330<\/code> has been removed and replaced\nwith <code class=\"language-plaintext highlighter-rouge\">glsl410<\/code> and <code class=\"language-plaintext highlighter-rouge\">glsl430<\/code>. When targeting the macOS GL backend, use <code class=\"language-plaintext highlighter-rouge\">glsl410<\/code>,\notherwise <code class=\"language-plaintext highlighter-rouge\">glsl430<\/code>.<\/p>\n\n<h2 id=\"a-simple-vertex-pulling-example\">A simple vertex pulling example<\/h2>\n\n<p>First let\u2019s rewrite the <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/cube-sapp.glsl\">cube-sapp.glsl<\/a> shader\nto pull vertices from a storage buffer instead of the fixed function vertex input.<\/p>\n\n<p>The original shader declares the vertex input with vertex attributes:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">position<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">in<\/span> <span class=\"kt\">vec4<\/span> <span class=\"n\">color0<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<blockquote>\n  <p>NOTE: the cube-sapp.glsl shader makes use of a fixed function vertex input\nfeature which extends float[3] vertex data on the CPU side to vec4 with a w-component 1.0\non the GPU side. Magic like this isn\u2019t supported when reading from storage buffers (as far as\nI\u2019m aware at least).<\/p>\n<\/blockquote>\n\n<p>For vertex pulling the input vertex attributes are replaced with a flexible-array struct inside a buffer interface block.<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">struct<\/span> <span class=\"n\">sb_vertex<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">vec3<\/span> <span class=\"n\">pos<\/span><span class=\"p\">;<\/span>\n    <span class=\"kt\">vec4<\/span> <span class=\"n\">color<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">};<\/span>\n\n<span class=\"n\">readonly<\/span> <span class=\"n\">buffer<\/span> <span class=\"n\">ssbo<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sb_vertex<\/span> <span class=\"n\">vtx<\/span><span class=\"p\">[];<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<blockquote>\n  <p>NOTE: I\u2019m using <code class=\"language-plaintext highlighter-rouge\">sb_vertex<\/code> for the struct name here because <code class=\"language-plaintext highlighter-rouge\">vertex<\/code> is a reserved keyword\nin the Metal Shading Language and would cause a compile error when outputting MSL.<\/p>\n<\/blockquote>\n\n<p>Do not use an attribute like <code class=\"language-plaintext highlighter-rouge\">layout(std430, binding=0)<\/code> for the buffer interface block,\nsokol-shdc will take care of those details.<\/p>\n\n<p>The original vertex shader looks like this:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">main<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nb\">gl_Position<\/span> <span class=\"o\">=<\/span> <span class=\"n\">mvp<\/span> <span class=\"o\">*<\/span> <span class=\"n\">position<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">color<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Converted to vertex pulling it looks like this:<\/p>\n\n<div class=\"language-glsl highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">main<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">vec4<\/span> <span class=\"n\">position<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">vec4<\/span><span class=\"p\">(<\/span><span class=\"n\">vtx<\/span><span class=\"p\">[<\/span><span class=\"n\">gl_VertexIndex<\/span><span class=\"p\">].<\/span><span class=\"n\">pos<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">);<\/span>\n    <span class=\"nb\">gl_Position<\/span> <span class=\"o\">=<\/span> <span class=\"n\">mvp<\/span> <span class=\"o\">*<\/span> <span class=\"n\">position<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">color<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vtx<\/span><span class=\"p\">[<\/span><span class=\"n\">gl_VertexIndex<\/span><span class=\"p\">].<\/span><span class=\"n\">color<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Note how <code class=\"language-plaintext highlighter-rouge\">gl_VertexIndex<\/code> (not <code class=\"language-plaintext highlighter-rouge\">gl_VertexID<\/code>!) is used to index into the storage buffer,\nthis is because sokol-shdc shaders are written in \u2018Vulkan style\u2019, not \u2018GL style\u2019.<\/p>\n\n<p>We also need to expand the vec3 input pos manually to a vec4 with w-component = 1.0.<\/p>\n\n<p>That\u2019s all the changes needed on the shader side. Next compile the modified shader with:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>sokol-shdc <span class=\"nt\">-i<\/span> shader.glsl <span class=\"nt\">-o<\/span> shader.h <span class=\"nt\">-l<\/span> metal_macos:hlsl5:glsl430:wgsl <span class=\"nt\">-f<\/span> sokol\n<\/code><\/pre><\/div><\/div>\n\n<p>Apart from the \u2018traditional\u2019 code-generation output, sokol-shdc will create two new\ndeclarations:<\/p>\n\n<ul>\n  <li>\n    <p>A define <code class=\"language-plaintext highlighter-rouge\">#define SLOT_ssbo (0)<\/code>, this is the bind slot index to be used in the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct<\/p>\n  <\/li>\n  <li>\n    <p>A C struct <code class=\"language-plaintext highlighter-rouge\">sb_vertex_t<\/code> which maps the GLSL struct <code class=\"language-plaintext highlighter-rouge\">sb_vertex<\/code> to the C side looking like this:<\/p>\n\n    <div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"n\">SOKOL_SHDC_ALIGN<\/span><span class=\"p\">(<\/span><span class=\"mi\">16<\/span><span class=\"p\">)<\/span> <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sb_vertex_t<\/span> <span class=\"p\">{<\/span>\n      <span class=\"kt\">float<\/span> <span class=\"n\">pos<\/span><span class=\"p\">[<\/span><span class=\"mi\">3<\/span><span class=\"p\">];<\/span>\n      <span class=\"kt\">uint8_t<\/span> <span class=\"n\">_pad_12<\/span><span class=\"p\">[<\/span><span class=\"mi\">4<\/span><span class=\"p\">];<\/span>\n      <span class=\"kt\">float<\/span> <span class=\"n\">color<\/span><span class=\"p\">[<\/span><span class=\"mi\">4<\/span><span class=\"p\">];<\/span>\n  <span class=\"p\">}<\/span> <span class=\"n\">sb_vertex_t<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n<\/ul>\n\n<blockquote>\n  <p>NOTE: with the right <code class=\"language-plaintext highlighter-rouge\">@ctype<\/code> tags at the top of the shader we could\nalso map the struct members to C or C++ types, for instance with HandmadeMath.h types:<\/p>\n<\/blockquote>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">SOKOL_SHDC_ALIGN<\/span><span class=\"p\">(<\/span><span class=\"mi\">16<\/span><span class=\"p\">)<\/span> <span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sb_vertex_t<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">hmm_vec3<\/span> <span class=\"n\">pos<\/span><span class=\"p\">;<\/span>\n    <span class=\"kt\">uint8_t<\/span> <span class=\"n\">_pad_12<\/span><span class=\"p\">[<\/span><span class=\"mi\">4<\/span><span class=\"p\">];<\/span>\n    <span class=\"n\">hmm_vec4<\/span> <span class=\"n\">color<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sb_vertex_t<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Next let\u2019s see how the <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/sapp\/cube-sapp.c\">cube-sapp C code<\/a> needs to be changed:<\/p>\n\n<p>The original code creates a vertex buffer like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">float<\/span> <span class=\"n\">vertices<\/span><span class=\"p\">[]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span>   <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n     <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span>   <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">};<\/span>\n<span class=\"n\">sg_buffer<\/span> <span class=\"n\">vbuf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">label<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"cube-vertices\"<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>By default <code class=\"language-plaintext highlighter-rouge\">sg_make_buffer()<\/code> creates a vertex buffer, so the above is\nidentical with a more explicit:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_buffer<\/span> <span class=\"n\">vbuf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">type<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_BUFFERTYPE_VERTEXBUFFER<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">label<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"cube-vertices\"<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026when changing the code to use storage buffers we can use the\ncode-generated <code class=\"language-plaintext highlighter-rouge\">sb_vertex_t<\/code> struct to initialize the vertex data.\nThis has the advantage that we don\u2019t need to care about the obscure\n<code class=\"language-plaintext highlighter-rouge\">std430<\/code> memory layout rules:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sb_vertex_t<\/span> <span class=\"n\">vertices<\/span><span class=\"p\">[]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">pos<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span> <span class=\"p\">},<\/span> <span class=\"p\">.<\/span><span class=\"n\">color<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span> <span class=\"p\">}<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">{<\/span> <span class=\"p\">.<\/span><span class=\"n\">pos<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>  <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"o\">-<\/span><span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span> <span class=\"p\">},<\/span> <span class=\"p\">.<\/span><span class=\"n\">color<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span> <span class=\"p\">}<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">...<\/span>\n<span class=\"p\">};<\/span>\n<span class=\"n\">sg_buffer<\/span> <span class=\"n\">sbuf<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_buffer<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_buffer_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">type<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_BUFFERTYPE_STORAGEBUFFER<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_RANGE<\/span><span class=\"p\">(<\/span><span class=\"n\">vertices<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">label<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"cube-vertices\"<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026note how the buffer type has changed to <code class=\"language-plaintext highlighter-rouge\">SG_BUFFERTYPE_STORAGEBUFFER<\/code>.<\/p>\n\n<p>On to the <code class=\"language-plaintext highlighter-rouge\">sg_pipeline<\/code> object. In the original code, a vertex layout\nmust be defined in the <code class=\"language-plaintext highlighter-rouge\">sg_pipeline_desc<\/code> struct to configure the\nfixed function vertex input stage:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">pip<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_pipeline<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pipeline_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">layout<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">attrs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_vs_position<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_VERTEXFORMAT_FLOAT3<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">[<\/span><span class=\"n\">ATTR_vs_color0<\/span><span class=\"p\">].<\/span><span class=\"n\">format<\/span>   <span class=\"o\">=<\/span> <span class=\"n\">SG_VERTEXFORMAT_FLOAT4<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">shader<\/span> <span class=\"o\">=<\/span> <span class=\"n\">shd<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_type<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_INDEXTYPE_UINT16<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">cull_mode<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_CULLMODE_BACK<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">write_enabled<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">compare<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_COMPAREFUNC_LESS_EQUAL<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">label<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"cube-pipeline\"<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>When pulling vertex data from storage buffers such a vertex layout description isn\u2019t needed, so\nthe pipeline creation can be simplified to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">pip<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_pipeline<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pipeline_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">shader<\/span> <span class=\"o\">=<\/span> <span class=\"n\">shd<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_type<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_INDEXTYPE_UINT16<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">cull_mode<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_CULLMODE_BACK<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">write_enabled<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">compare<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_COMPAREFUNC_LESS_EQUAL<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">label<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"cube-pipeline\"<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026the original <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct that\u2019s passed into <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">bind<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vertex_buffers<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">vbuf<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"n\">ibuf<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026is changed like this (e.g. replace the vertex buffer binding with a storage\nbuffer binding on the vertex shader stage):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">bind<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_bindings<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">index_buffer<\/span> <span class=\"o\">=<\/span> <span class=\"n\">ibuf<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">vs<\/span><span class=\"p\">.<\/span><span class=\"n\">storage_buffers<\/span><span class=\"p\">[<\/span><span class=\"n\">SLOT_ssbo<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sbuf<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and that\u2019s it! On the CPU side, storage buffers actually simplify a lot of code because\nyou don\u2019t need a vertex layout in the <code class=\"language-plaintext highlighter-rouge\">sg_pipeline_desc<\/code> struct, and you get a properly\naligned and padded C struct for the storage buffer content from sokol-shdc.<\/p>\n\n<blockquote>\n  <p>NOTE: A \u2018proper\u2019 cross-backend sample should also check whether storage buffers are\nactually supported via <code class=\"language-plaintext highlighter-rouge\">sg_query_features().storage_buffer<\/code> and render some\nsort of fallback.<\/p>\n<\/blockquote>\n\n<h2 id=\"shader-authoring-caveats\">Shader Authoring Caveats<\/h2>\n\n<p>Shader authoring via sokol-shdc is a bit more restricted than vanilla GLSL:<\/p>\n\n<ol>\n  <li>A storage buffer interface block must contain exactly one item, and this\n  item must be a flexible struct array member. In vanilla GLSL you can have\n  additional \u2018header items\u2019 in front of the flexible array member, but this\n  turned out tricky to map to CPU-side non-C languages that don\u2019t allow\n  flexible array members (I actually need to research the various target languages a bit more, maybe\n  this rule can be relaxed in the future for some of the target languages).<\/li>\n  <li>Currently the following types are valid inside a storage buffer struct:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">bool, bvec2..4<\/code>: mapped to int32_t, and int32_t[2..4]<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">int, ivec2..4<\/code>: mapped to int32_t, and int32_t[2..4]<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">uint, uvec2..4<\/code>: mapped to uint32_t, and uint32_t[2..4]<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">float, vec2..4<\/code>: mapped to float and float[2..4]<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">matNxM<\/code> where N=2..4 and M=1..4 mapped to float[2..64]<\/li>\n    <\/ul>\n  <\/li>\n  <li>nested structs<\/li>\n  <li>arrays of the above<\/li>\n<\/ol>\n\n<p>Please note that only few of those combinations are tested, especially when it\ncomes to correct array item padding and alignment. If you stumble over any problems\nplease write a ticket at <a href=\"https:\/\/github.com\/floooh\/sokol-tools\/issues\">https:\/\/github.com\/floooh\/sokol-tools\/issues<\/a>.<\/p>\n\n<p>To load packed vertex components from storage buffers, use the following GLSL builtins:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">vec2 unpackUnorm2x16(uint p)<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">vec2 unpackSnorm2x16(uint p)<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">vec4 unpackUnorm4x8(uint p)<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">vec4 unpackSnorm4x8(uint p)<\/code><\/li>\n<\/ul>\n\n<h2 id=\"under-the-hood\">Under the hood<\/h2>\n\n<blockquote>\n  <p>NOTE: the following information about shader bind slots are only relevant if\nyou do not use the sokol shader compiler (sokol-shdc), but instead pass \u2018raw\u2019\nHLSL, MSL, GLSL or WGSL shaders into sokol_gfx.h. Also, this information will\nbecome obsolete\/irrelevant with another future update I have in mind which will allow\nmore flexibility when mapping sokol-gfx bind slots to backend 3D API bind slots\n(see this planning ticket for more info: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1037\">#1037<\/a>)<\/p>\n<\/blockquote>\n\n<h3 id=\"metal\">Metal<\/h3>\n\n<p>On Metal there is no \u2018buffer zoo\u2019 like in other 3D APIs, uniform-, vertex-,\nindex- and storage-buffers are all the same thing. The vertex-\nand fragment-shader stages have their own buffer bind slot spaces though.<\/p>\n\n<p>The following bind slot ranges are used for the various sokol-gfx\nbuffer types:<\/p>\n\n<ul>\n  <li>on the vertex shader stage:\n    <ul>\n      <li><strong><code class=\"language-plaintext highlighter-rouge\">slots 0..3<\/code><\/strong> for uniform buffer bindings (sokol-gfx internally\nmanages an uniform buffer which might be bound at up to four different\noffsets)<\/li>\n      <li><strong><code class=\"language-plaintext highlighter-rouge\">slots 4..11<\/code><\/strong> for vertex buffer bindings<\/li>\n      <li><strong><code class=\"language-plaintext highlighter-rouge\">slots 12..19<\/code><\/strong> for storage buffer bindings<\/li>\n    <\/ul>\n  <\/li>\n  <li>on the fragment shader stage:\n    <ul>\n      <li><strong><code class=\"language-plaintext highlighter-rouge\">slots 0..3<\/code><\/strong> for uniform buffer bindings<\/li>\n      <li><strong><code class=\"language-plaintext highlighter-rouge\">slots 4..11<\/code><\/strong> for storage buffer bindings<\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>When authoring Metal shaders directly you\u2019ll need to use the above bind slots\n(also see the low-level <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal\">Metal backend samples<\/a>).<\/p>\n\n<h3 id=\"d3d11\">D3D11<\/h3>\n\n<p>On D3D11, so called <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/direct3d11\/overviews-direct3d-11-resources-intro#raw-views-of-buffers\"><em>Byte Address\nBuffers<\/em><\/a>\nare used for storage buffers which makes their direct usage in manually written\nHLSL a bit awkward (but is not an issue when using sokol-shdc).<\/p>\n\n<p>If this turns out to be a problem I might add D3D11-specific creation flags to\n<code class=\"language-plaintext highlighter-rouge\">sg_buffer_desc<\/code> to allow using different D3D11 buffer and buffer-view types\nunder the hood, details like this might also change again once compute shader\nsupport is added.<\/p>\n\n<p>On D3D11 and HLSL storage buffers share a bind slot range with texture bindings, that\u2019s\nwhy sokol-gfx defines the following bind ranges for textures and storage buffers in\nHLSL:<\/p>\n\n<ul>\n  <li><strong><code class=\"language-plaintext highlighter-rouge\">register(t0..t15)<\/code><\/strong>: reserved for texture bindings<\/li>\n  <li><strong><code class=\"language-plaintext highlighter-rouge\">register(t16..t23)<\/code><\/strong>: reserved for storage buffer bindings<\/li>\n<\/ul>\n\n<p>Also see the low-level <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11\">D3D11 backend samples<\/a> for details.<\/p>\n\n<h3 id=\"webgpu\">WebGPU<\/h3>\n\n<p>Storage buffers are created with <code class=\"language-plaintext highlighter-rouge\">WGPUBufferUsage_Storage<\/code>. WebGPU uses a common bind slot\nspace across all shader resource types and shader stages. Sokol-gfx reserves the following bind\nslot ranges for the different shader stages and resource types, use those when feeding manually\nwritten WGSL shaders into sokol-gfx:<\/p>\n\n<ul>\n  <li>vertex shader stage:\n    <ul>\n      <li>textures: <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(0..15)<\/code><\/strong><\/li>\n      <li>samplers: <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(16..31)<\/code><\/strong><\/li>\n      <li>storage buffers; <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(32..47)<\/code><\/strong><\/li>\n    <\/ul>\n  <\/li>\n  <li>fragment shader stage:\n    <ul>\n      <li>textures: <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(48..63)<\/code><\/strong><\/li>\n      <li>samplers: <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(64..79)<\/code><\/strong><\/li>\n      <li>storage buffers: <strong><code class=\"language-plaintext highlighter-rouge\">@group(1) @binding(80..95)<\/code><\/strong><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>Also see the low-level <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu\">WebGPU backend samples<\/a> for details<\/p>\n\n<h3 id=\"gl\">GL<\/h3>\n\n<p>In GL, storage buffers are bound to the <code class=\"language-plaintext highlighter-rouge\">GL_SHADER_STORAGE_BUFFER<\/code> target. Sokol-gfx\ndoes not lookup GLSL storage buffer interface blocks by name, but instead expects that\nthe GLSL code that\u2019s passed into <code class=\"language-plaintext highlighter-rouge\">sg_make_shader()<\/code> uses a <code class=\"language-plaintext highlighter-rouge\">layout(std430, binding=N)<\/code>\nannotation to define the bind slot.<\/p>\n\n<p>The vertex- and fragment-shader stage use a common bind space:<\/p>\n\n<ul>\n  <li>on the vertex shader stage, use <strong><code class=\"language-plaintext highlighter-rouge\">binding 0..7<\/code><\/strong><\/li>\n  <li>on the fragment shader stage, use <strong><code class=\"language-plaintext highlighter-rouge\">binding 7..15<\/code><\/strong><\/li>\n<\/ul>\n\n<p>Also see the low-level <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/storage-buffers\/glfw\">desktop GL backend samples<\/a> for details.<\/p>\n\n<h2 id=\"sokol-shdc-updates\">sokol-shdc updates<\/h2>\n\n<p>Sokol-shdc has been massively refactored, mainly with the goal to have a more\nrobust base for extracting reflection information from shaders and a more\n\u2018structured\u2019 approach to code generation so that supporting additional CPU-side\nlanguages will be easier in the future (I\u2019m not yet sure if that last goal was actually achieved though, but time will tell).<\/p>\n\n<p>Unfortunately this massive refactoring also means that there\u2019s a possibility that new\nbugs have sneaked in. If you notice anything weird, please write tickets here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-tools\/issues\">https:\/\/github.com\/floooh\/sokol-tools\/issues<\/a>.<\/p>\n\n<p>A couple of unrelated lingering bugs have been fixed as well:<\/p>\n\n<ul>\n  <li>C++ exceptions are now enabled and exceptions coming out of SPIRVCross are now caught\nand turned into proper error messages. Previously sokol-shdc would simply appear to\ncrash if SPIRVCross emitted an error (because without C++ exceptions enabled,\nthose errors would be turned into a panic which looks like a segfault).<\/li>\n  <li>Error and warning line numbers had been off by a couple of lines recently.\nThis has been fixed and error messages now point to the correct line again.<\/li>\n  <li>A couple of somewhat esoteric code generation bugs in non-C code generators\nwere fixed (but as I said, it\u2019s also quite likely that I have introduced new bugs\nin that area, since code generators were completely rewritten)<\/li>\n<\/ul>\n\n<h2 id=\"whats-next\">What\u2019s next:<\/h2>\n\n<p>In short:<\/p>\n\n<ul>\n  <li>A resource binding cleanup (see\n<a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/1037\">#1037<\/a>), the main motivation\nfor this is that the <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code> struct is growing quite large and would\ngrow even larger if a new compute shader stage is added. Furthermore, the artificial\nseparation of shader stages when binding resources also doesn\u2019t map\nparticularly well to some modern 3D APIs.<\/li>\n  <li>After that it\u2019s finally time to tackle compute shaders. For this I need to\ncome up with a resource synchronization strategy, but I will most likely\njust copy what WebGPU does.<\/li>\n<\/ul>\n\n<p>But first I will probably take a little break and dabble a bit with Zig and emulator coding :)<\/p>\n","pubDate":"Mon, 06 May 2024 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2024\/05\/06\/sokol-storage-buffers.html","guid":"https:\/\/floooh.github.io\/2024\/05\/06\/sokol-storage-buffers.html"},{"title":"Upcoming Sokol header API changes (Feb 2024)","description":"<p>In a couple of days I will merge the first big API update of 2024 for sokol_gfx.h (with some\nrelated changes in sokol_app.h, sokol_glue.h and sokol_gfx_imgui.h).<\/p>\n\n<blockquote>\n  <p>NOTE: most links to code examples will only point to the right code after <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/985\">PR #985<\/a> has been merged!<\/p>\n<\/blockquote>\n\n<p>The API update in sokol_gfx.h is a <strong>BREAKING CHANGE<\/strong> for all code, but for most use cases\nthe required changes are fairly minimal.<\/p>\n\n<p>Apologies for the broken syntax highlighting, apparently <a href=\"https:\/\/github.com\/rouge-ruby\/rouge\">Rouge<\/a> doesn\u2019t understand C99.<\/p>\n\n<h2 id=\"table-of-contents\">Table of Contents<\/h2>\n\n<ul id=\"markdown-toc\">\n  <li><a href=\"#table-of-contents\" id=\"markdown-toc-table-of-contents\">Table of Contents<\/a><\/li>\n  <li><a href=\"#overview-and-motivation\" id=\"markdown-toc-overview-and-motivation\">Overview and Motivation<\/a><\/li>\n  <li><a href=\"#detailed-change-list\" id=\"markdown-toc-detailed-change-list\">Detailed change list<\/a>    <ul>\n      <li><a href=\"#sokol_gfxh\" id=\"markdown-toc-sokol_gfxh\">sokol_gfx.h<\/a><\/li>\n      <li><a href=\"#sokol_apph\" id=\"markdown-toc-sokol_apph\">sokol_app.h<\/a><\/li>\n      <li><a href=\"#sokol_glueh\" id=\"markdown-toc-sokol_glueh\">sokol_glue.h<\/a><\/li>\n      <li><a href=\"#sokol_gfx_imguih\" id=\"markdown-toc-sokol_gfx_imguih\">sokol_gfx_imgui.h<\/a><\/li>\n    <\/ul>\n  <\/li>\n  <li><a href=\"#link-collection-with-example-code-changes\" id=\"markdown-toc-link-collection-with-example-code-changes\">Link collection with example code changes<\/a><\/li>\n  <li><a href=\"#detailed-change-recipes\" id=\"markdown-toc-detailed-change-recipes\">Detailed Change Recipes<\/a>    <ul>\n      <li><a href=\"#for-sokol_gfxh--sokol_apph--sokol_glueh\" id=\"markdown-toc-for-sokol_gfxh--sokol_apph--sokol_glueh\">\u2026for sokol_gfx.h + sokol_app.h + sokol_glue.h<\/a><\/li>\n      <li><a href=\"#for-offscreen-render-passes\" id=\"markdown-toc-for-offscreen-render-passes\">\u2026for offscreen render passes<\/a><\/li>\n      <li><a href=\"#for-custom-window-system-glue\" id=\"markdown-toc-for-custom-window-system-glue\">\u2026for custom window system glue<\/a>        <ul>\n          <li><a href=\"#using-d3d11\" id=\"markdown-toc-using-d3d11\">\u2026using D3D11<\/a><\/li>\n          <li><a href=\"#using-metal\" id=\"markdown-toc-using-metal\">\u2026using Metal<\/a><\/li>\n          <li><a href=\"#using-webgpu\" id=\"markdown-toc-using-webgpu\">\u2026using WebGPU<\/a><\/li>\n          <li><a href=\"#gl-with-glfw\" id=\"markdown-toc-gl-with-glfw\">\u2026GL with GLFW<\/a><\/li>\n        <\/ul>\n      <\/li>\n    <\/ul>\n  <\/li>\n  <li><a href=\"#q-why-still-have-a-baked-pass-attachments-object\" id=\"markdown-toc-q-why-still-have-a-baked-pass-attachments-object\">Q: Why still have a baked pass attachments object?<\/a><\/li>\n<\/ul>\n\n<h2 id=\"overview-and-motivation\">Overview and Motivation<\/h2>\n\n<p>The general topic of this update is a cleanup of the sokol-gfx render pass\nfunctions and how external swapchain information is passed into sokol-gfx.<\/p>\n\n<p>Previously there was a special \u2018default render pass\u2019 into a \u2018default framebuffer\u2019,\nand the concept of \u2018contexts\u2019 to allow switching between different rendering contexts\nand their default framebuffers (very similar to traditional OpenGL contexts,\nand in fact this old behavior only ever matched OpenGL, but not the other backend\nAPIs).<\/p>\n\n<p>This setup was needlessly complicated for people who want to use sokol-gfx\nto render into multiple windows, leading to planning <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/904\">ticket #904<\/a>,\nand then to <a href=\"https:\/\/github.com\/floooh\/sokol\/pull\/985\">PR #985<\/a>.<\/p>\n\n<p>The gist is:<\/p>\n\n<ul>\n  <li>There is now only a single \u2018unified\u2019 <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> function which covers\nboth rendering into sokol-gfx render target textures (aka \u2018offscreen passes\u2019)\nand externally managed \u2018swapchains\u2019 (aka \u2018swapchain passes\u2019).<\/li>\n  <li>The entire concept of <code class=\"language-plaintext highlighter-rouge\">contexts<\/code> has been removed from sokol_gfx.h.<\/li>\n  <li>External swapchain properties are now passed directly into <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code>\nin a transient structure.<\/li>\n<\/ul>\n\n<p>Instead of having a special and unique \u2018default-render-pass\u2019 per frame and context,\nan application can now simply call <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> multiple times per frame,\neach time with properties for a different swapchain, and all that without having to\ncreate \u2018context objects\u2019 upfront or \u2018switching contexts\u2019.<\/p>\n\n<p>Most simple applications that don\u2019t render into offscreen passes and\nuse sokol_gfx.h together with sokol_app.h and sokol_glue.h only need to change\ntwo calls: <code class=\"language-plaintext highlighter-rouge\">sg_setup()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_begin_default_pass()<\/code>, for other situations\nplease check the \u2018Change Recipes\u2019 section further down.<\/p>\n\n<p>In addition to this blog post, please also re-read the documentation headers\nin sokol_gfx.h and sokol_app.h, and specifically the struct documentation\nfor the new sokol-gfx structs <code class=\"language-plaintext highlighter-rouge\">sg_environment<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code>.<\/p>\n\n<h2 id=\"detailed-change-list\">Detailed change list<\/h2>\n\n<h3 id=\"sokol_gfxh\">sokol_gfx.h<\/h3>\n\n<p>The following public API structs and functions have been <strong>removed<\/strong>:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_begin_default_pass()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_begin_default_passf()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">struct sg_context_desc<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">struct sg_context<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_setup_context()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_activate_context()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_discard_context()<\/code><\/li>\n<\/ul>\n\n<p>The following top-level structs have been <strong>added<\/strong>:<\/p>\n\n<ul>\n  <li>\n    <p><code class=\"language-plaintext highlighter-rouge\">struct sg_environment<\/code>: this is passed as a nested struct of <code class=\"language-plaintext highlighter-rouge\">sg_desc<\/code> into\nthe <code class=\"language-plaintext highlighter-rouge\">sg_setup()<\/code> call to provide information about the environment sokol-gfx\nruns in (most importantly 3D API device pointers).<\/p>\n  <\/li>\n  <li>\n    <p><code class=\"language-plaintext highlighter-rouge\">struct sg_swapchain<\/code>: this is passed into <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> for render passes\nwhich should render into an externally managed swapchain. The struct contains the\nfollowing information:<\/p>\n    <ul>\n      <li>the pixel format of the swapchain\u2019s rendering surface<\/li>\n      <li>the pixel format of the optional depth\/stencil surface<\/li>\n      <li>an MSAA sample count<\/li>\n      <li>3D backend specific resource handles, like D3D11\/WebGPU texture views, Metal drawables,\nor GL framebuffers<\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>The resource handle type <code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code> has been <strong>renamed<\/strong> to <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> (to\nfree the name for another purpose), this also causes related renames:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_pass_desc<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_attachments_desc<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_pass_info<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_attachments_info<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_make_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_make_attachments()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_destroy_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_destroy_attachments()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_query_pass_state()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_query_attachments_state()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_query_pass_info()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_query_attachments_info()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_query_pass_desc()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_query_attachments_desc()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_alloc_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_alloc_attachments()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_dealloc_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_dealloc_attachments()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_init_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_init_attachments()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_fail_pass()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_fail_attachments()<\/code><\/li>\n  <li>\n    <table>\n      <tbody>\n        <tr>\n          <td><code class=\"language-plaintext highlighter-rouge\">sg_[*]_pass_info()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sg_[*]_attachments_info()<\/code> (where \u2018*\u2019 is \u2018d3d11<\/td>\n          <td>gl<\/td>\n          <td>metal<\/td>\n          <td>wgpu\u2019)<\/td>\n        <\/tr>\n      <\/tbody>\n    <\/table>\n  <\/li>\n<\/ul>\n\n<p>Inside the <code class=\"language-plaintext highlighter-rouge\">sg_attachments_desc<\/code> struct there has been some renaming to reduce redundancy:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">.color_attachments[]<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">.colors[]<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">.resolve_attachments[]<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">.resolves[]<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">.depth_stencil_attachment<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">.depth_stencil<\/code><\/li>\n<\/ul>\n\n<p>The typename <code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code> has been repurposed to serve as the <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> parameter,\ne.g. the begin-pass function signature now looks like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"k\">const<\/span> <span class=\"n\">sg_pass<\/span><span class=\"o\">*<\/span> <span class=\"n\">pass<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>With the struct <code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code> now looking like this (with omitted start\/end canaries):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">typedef<\/span> <span class=\"k\">struct<\/span> <span class=\"n\">sg_pass<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sg_pass_action<\/span> <span class=\"n\">action<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">sg_attachments<\/span> <span class=\"n\">attachments<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">sg_swapchain<\/span> <span class=\"n\">swapchain<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">char<\/span><span class=\"o\">*<\/span> <span class=\"n\">label<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"n\">sg_pass<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>For an \u2018offscreen-render-pass\u2019, an <code class=\"language-plaintext highlighter-rouge\">.attachments<\/code> item must be provided, but no\n<code class=\"language-plaintext highlighter-rouge\">.swapchain<\/code>:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"n\">attachments<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and for a \u2018swapchain-render-pass\u2019, a <code class=\"language-plaintext highlighter-rouge\">.swapchain<\/code> item must be provided, but no\n<code class=\"language-plaintext highlighter-rouge\">.attachments<\/code>:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">swapchain<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sglue_swapchain<\/span><span class=\"p\">(),<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Other unrelated \u2018drive-by-changes\u2019 in sokol_gfx.h:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sg_limits.gl_max_vertex_uniform_vectors<\/code> has been replaced with <code class=\"language-plaintext highlighter-rouge\">sg_limits.gl_max_vertex_uniform_components<\/code>\n(see <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/714\">#714<\/a>)<\/li>\n  <li>the start and end canaries in <code class=\"language-plaintext highlighter-rouge\">sg_pass_action<\/code> have been removed (since <code class=\"language-plaintext highlighter-rouge\">sg_pass_action<\/code> is now a nested\nstruct of <code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code>, the canaries are redundant)<\/li>\n  <li>a new initialization config item <code class=\"language-plaintext highlighter-rouge\">sg_desc.mtl_use_command_buffer_with_retained_references<\/code> has been added,\n(see: <a href=\"https:\/\/github.com\/floooh\/sokol\/issues\/981\">#981<\/a>)<\/li>\n<\/ul>\n\n<h3 id=\"sokol_apph\">sokol_app.h<\/h3>\n\n<p>The following public API function has been removed:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_metal_get_renderpass_descriptor()<\/code><\/li>\n<\/ul>\n\n<p>The following functions have been renamed:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_metal_get_drawable()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sapp_metal_get_current_drawable()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_d3d11_get_render_target_view()<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">sapp_d3d11_get_render_view()<\/code><\/li>\n<\/ul>\n\n<p>\u2026and the following functions are new:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_metal_get_depth_stencil_texture()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_metal_get_msaa_color_texture()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_d3d11_get_resolve_view()<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sapp_gl_get_framebuffer()<\/code><\/li>\n<\/ul>\n\n<p>\u2026These functions directly plug into the new <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code> struct in sokol_gfx.h.<\/p>\n\n<h3 id=\"sokol_glueh\">sokol_glue.h<\/h3>\n\n<p>sokol_glue.h is now a regular library header without the \u2018preprocessor magic\u2019\nwhich created a different API depending on what other sokol headers had been\nincluded before sokol_glue.h (this was an \u2018interesting\u2019 but ultimately\npretty stupid idea).<\/p>\n\n<p>The API prefix has changed from a somewhat confusing <code class=\"language-plaintext highlighter-rouge\">sapp_<\/code> to the expected <code class=\"language-plaintext highlighter-rouge\">sglue_<\/code>.<\/p>\n\n<p>The old function <code class=\"language-plaintext highlighter-rouge\">sapp_sgcontext()<\/code> has been split into two new functions:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">sglue_environment()<\/code> which plugs directly into <code class=\"language-plaintext highlighter-rouge\">sg_desc.environment<\/code>, and\u2026<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">sglue_swapchain()<\/code> which plugs into <code class=\"language-plaintext highlighter-rouge\">sg_pass.swapchain<\/code><\/li>\n<\/ul>\n\n<p>Note that <code class=\"language-plaintext highlighter-rouge\">sglue_swapchain()<\/code> may return different values each frame depending\non the 3D API backend.<\/p>\n\n<h3 id=\"sokol_gfx_imguih\">sokol_gfx_imgui.h<\/h3>\n\n<p>In a similar vein, the public API prefix of sokol_gfx_imgui.h has been changed from the\nweird \u2018double prefix\u2019 <code class=\"language-plaintext highlighter-rouge\">sg_imgui_<\/code> to a more conventional <code class=\"language-plaintext highlighter-rouge\">sgimgui_<\/code>.<\/p>\n\n<p>Apart from this publicly visible change, all the internals have been updated to reflect\nthe sokol-gfx API changes.<\/p>\n\n<h2 id=\"link-collection-with-example-code-changes\">Link collection with example code changes<\/h2>\n\n<p>If you use sokol_gfx.h + sokol_app.h + sokol_glue.h, check out the updated samples\nhere (first click on a sample, and then on the \u2018src\u2019 link at the bottom):<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/floooh.github.io\/sokol-html5\/\">sokol samples<\/a><\/li>\n<\/ul>\n\n<p>Specifically look at <a href=\"https:\/\/floooh.github.io\/sokol-html5\/clear-sapp.html\">clear-sapp<\/a>\nfor the simple case of only rendering to a default framebuffer, and\n<a href=\"https:\/\/floooh.github.io\/sokol-html5\/offscreen-sapp.html\">offscreen-sapp<\/a> for\nrendering to an offscreen render target.<\/p>\n\n<p>If you use sokol_gfx.h with your own window system glue, or a library like GLFW or SDL,\ncheck out the updated backend specific examples:<\/p>\n\n<ul>\n  <li>for D3D11: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/d3d11<\/a><\/li>\n  <li>for Metal: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/metal<\/a><\/li>\n  <li>for GL with GLFW: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/glfw<\/a><\/li>\n  <li>for WebGL2: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/html5<\/a><\/li>\n  <li>for WebGPU: <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu\">https:\/\/github.com\/floooh\/sokol-samples\/tree\/master\/wgpu<\/a><\/li>\n<\/ul>\n\n<p>The GLFW subdirectory also contains an updated <code class=\"language-plaintext highlighter-rouge\">multiwindow-glfw<\/code> sample, and\na <code class=\"language-plaintext highlighter-rouge\">metal-glfw<\/code> sample which demonstrates how to use GLFW in NO_API mode together\nwith the sokol_gfx.h Metal backend.<\/p>\n\n<p>Also please be aware of the following behaviour and expectation changes if you\nare using your own window system glue:<\/p>\n\n<ul>\n  <li>\n    <p>For <strong>D3D11\/DXGI<\/strong> the MSAA resolve operation is now performed in <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code>,\npreviously this was expected to be performed in the window system glue before\npresentation.<\/p>\n  <\/li>\n  <li>\n    <p>For <strong>Metal<\/strong> it is now expected that the window system glue provides\na <code class=\"language-plaintext highlighter-rouge\">CAMetalDrawable<\/code> and optional <code class=\"language-plaintext highlighter-rouge\">MTLTexture<\/code> objects instead of an\n<code class=\"language-plaintext highlighter-rouge\">MTLRenderPassDescriptor<\/code>. This was also done to better \u2018harmonize\u2019\nwith the other backends (it\u2019s just as easy getting those individual\nobjects from an <code class=\"language-plaintext highlighter-rouge\">MTKView<\/code> as the <code class=\"language-plaintext highlighter-rouge\">MTLRenderPassDescriptor<\/code>).<\/p>\n  <\/li>\n  <li>\n    <p>For <strong>GL<\/strong>, sokol-gfx now expects that <em>all<\/em> rendering goes through a single\nGL context. This may require changes to existing code which renders into\nmultiple windows (for instance in GLFW, every window has its own GL context).\nRefer to the new\n<a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/multiwindow-glfw.c\">multiwindow-glfw.c<\/a>\nexample for a possible solution.<\/p>\n  <\/li>\n<\/ul>\n\n<p>Additionally, check out the following PRs for required changes in my toy\nprojects:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/pacman.c\/pull\/12\">pacman.c<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/doom-sokol\/pull\/1\">Doom on Sokol<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/v6502r\/pull\/24\/files\">Visual 6502 Remix<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/qoiview\/pull\/10\">qoiview<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/chips-test\/pull\/33\">chips<\/a><\/li>\n<\/ul>\n\n<p>When using the language bindings, check out the following PRs:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-zig\/pull\/57\/files\">sokol-zig<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-odin\/pull\/8\">sokol-odin<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-nim\/pull\/28\">sokol-nim<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/sokol-rust\/pull\/22\">sokol-rust<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/pacman.zig\/pull\/23\">pacman.zig<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/floooh\/kc85.zig\/pull\/4\">kc85.zig<\/a><\/li>\n<\/ul>\n\n<h2 id=\"detailed-change-recipes\">Detailed Change Recipes<\/h2>\n\n<h3 id=\"for-sokol_gfxh--sokol_apph--sokol_glueh\">\u2026for sokol_gfx.h + sokol_app.h + sokol_glue.h<\/h3>\n\n<p>When using sokol_gfx.h together with sokol_app.h and sokol_glue.h\u2026<\/p>\n\n<p>\u2026change your <code class=\"language-plaintext highlighter-rouge\">sg_setup()<\/code> call from this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_setup<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">context<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sapp_sgcontext<\/span><span class=\"p\">(),<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">logger<\/span><span class=\"p\">.<\/span><span class=\"n\">func<\/span> <span class=\"o\">=<\/span> <span class=\"n\">slog_func<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_setup<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">environment<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sglue_environment<\/span><span class=\"p\">(),<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">logger<\/span><span class=\"p\">.<\/span><span class=\"n\">func<\/span> <span class=\"o\">=<\/span> <span class=\"n\">slog_func<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Change the <code class=\"language-plaintext highlighter-rouge\">sg_begin_default_pass()<\/code> call from this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_default_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span> <span class=\"n\">sapp_width<\/span><span class=\"p\">(),<\/span> <span class=\"n\">sapp_height<\/span><span class=\"p\">());<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">swapchain<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sglue_swapchain<\/span><span class=\"p\">()<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"for-offscreen-render-passes\">\u2026for offscreen render passes<\/h3>\n\n<p>Change <code class=\"language-plaintext highlighter-rouge\">sg_make_pass()<\/code> calls from this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_pass<\/span> <span class=\"n\">pass<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">color_attachments<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">resolve_attachments<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">resolve_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_attachment<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">depth_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_attachments<\/span> <span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sg_make_attachments<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_attachments_desc<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">color_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">resolves<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">resolve_img<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">depth_stencil<\/span><span class=\"p\">.<\/span><span class=\"n\">image<\/span> <span class=\"o\">=<\/span> <span class=\"n\">depth_img<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Change <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> calls from this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"n\">pass<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">pass_action<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026to this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">.<\/span><span class=\"n\">attachments<\/span> <span class=\"o\">=<\/span> <span class=\"n\">attachments<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"for-custom-window-system-glue\">\u2026for custom window system glue<\/h3>\n\n<p>Create two helper functions, one which returns an initialized <code class=\"language-plaintext highlighter-rouge\">sg_environment<\/code>\nstruct and one which returns an initialized <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code> struct. Following\nare examples how these functions might look like for different backend 3D APIs.<\/p>\n\n<h4 id=\"using-d3d11\">\u2026using D3D11<\/h4>\n\n<p>Example implementations:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_environment<\/span> <span class=\"nf\">d3d11_environment<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_environment<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">defaults<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_BGRA8<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">d3d11<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">device<\/span> <span class=\"o\">=<\/span> <span class=\"n\">d3d11_device<\/span><span class=\"p\">,<\/span> <span class=\"c1\">\/\/ ID3D11Device*<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">device_context<\/span> <span class=\"o\">=<\/span> <span class=\"n\">d3d11_device_context<\/span><span class=\"p\">,<\/span> <span class=\"c1\">\/\/ ID3D11DeviceContext*<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p><code class=\"language-plaintext highlighter-rouge\">.defaults.color_format<\/code>, <code class=\"language-plaintext highlighter-rouge\">defaults.depth_format<\/code> and <code class=\"language-plaintext highlighter-rouge\">defaults.sample_count<\/code>\nshould match the \u2018most common\u2019 swapchain surface properties. These defaults\nwill be used to fill in defaults for zero-initialized values in various\nsokol-gfx calls. <code class=\"language-plaintext highlighter-rouge\">.depth_format<\/code> can also be <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_NONE<\/code> if no\ndepth-buffer exists, or <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH<\/code> if no stencil buffer is used.<\/p>\n\n<p>The associated DXGI depth-stencil-view pixel formats are:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH_STENCIL<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">DXGI_FORMAT_D24_UNORM_S8_UINT<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">DXGI_FORMAT_D32_FLOAT<\/code><\/li>\n<\/ul>\n\n<p>The helper function to obtain an <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code> struct might look like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_swapchain<\/span> <span class=\"nf\">d3d11_swapchain<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_swapchain<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">width<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">height<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_BGRA8<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">d3d11<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">render_view<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"o\">?<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">rt_view<\/span> <span class=\"o\">:<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">msaa_view<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">resolve_view<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"o\">?<\/span> <span class=\"mi\">0<\/span> <span class=\"o\">:<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">rt_view<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">ds_view<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p><code class=\"language-plaintext highlighter-rouge\">state.rt_view<\/code> and <code class=\"language-plaintext highlighter-rouge\">state.msaa_view<\/code> are of type <code class=\"language-plaintext highlighter-rouge\">ID3D11RenderTargetView<\/code> and <code class=\"language-plaintext highlighter-rouge\">state.ds_view<\/code> is\nof type <code class=\"language-plaintext highlighter-rouge\">ID3D11DepthStencilView<\/code>.<\/p>\n\n<p>Note how a different <code class=\"language-plaintext highlighter-rouge\">.d3d11.render_view<\/code> is selected depending on whether multisampled rendering\nis used or not. For non-multisampled rendering, sokol-gfx renders into the same view that\u2019s\npresented. For multisampled rendering, sokol-gfx will render into an intermediate MSAA texture\nview (<code class=\"language-plaintext highlighter-rouge\">state.msaa_view<\/code>) which is then resolved into the <code class=\"language-plaintext highlighter-rouge\">d3d11.resolve_view<\/code> inside\n<code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code>.<\/p>\n\n<p>Also check out the example D3D11 window system glue code here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/d3d11\/d3d11entry.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/d3d11\/d3d11entry.c<\/a><\/p>\n\n<h4 id=\"using-metal\">\u2026using Metal<\/h4>\n\n<p>Example function which returns an initialized <code class=\"language-plaintext highlighter-rouge\">sg_environment<\/code> struct:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_environment<\/span> <span class=\"nf\">osx_environment<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_environment<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">defaults<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sample_count<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_BGRA8<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">metal<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">device<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">__bridge<\/span> <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">)<\/span> <span class=\"n\">mtl_device<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The ObjC type of <code class=\"language-plaintext highlighter-rouge\">mtl_device<\/code> is <code class=\"language-plaintext highlighter-rouge\">id&lt;MTLDevice&gt;<\/code>. Note the special <code class=\"language-plaintext highlighter-rouge\">__bridge<\/code> cast to\na void pointer for tunneling through the sokol_app.h and sokol_gfx.h C APIs.<\/p>\n\n<p>\u2026and the function which returns an <code class=\"language-plaintext highlighter-rouge\">sg_swapchain<\/code> struct (in this case using an <code class=\"language-plaintext highlighter-rouge\">MTKView<\/code>\nto manage the swapchain surfaces):<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_swapchain<\/span> <span class=\"nf\">osx_swapchain<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_swapchain<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"kt\">int<\/span><span class=\"p\">)<\/span> <span class=\"p\">[<\/span><span class=\"n\">mtk_view<\/span> <span class=\"n\">drawableSize<\/span><span class=\"p\">].<\/span><span class=\"n\">width<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"kt\">int<\/span><span class=\"p\">)<\/span> <span class=\"p\">[<\/span><span class=\"n\">mtk_view<\/span> <span class=\"n\">drawableSize<\/span><span class=\"p\">].<\/span><span class=\"n\">height<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sample_count<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_BGRA8<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">metal<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">current_drawable<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">__bridge<\/span> <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">)<\/span> <span class=\"p\">[<\/span><span class=\"n\">mtk_view<\/span> <span class=\"n\">currentDrawable<\/span><span class=\"p\">],<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">__bridge<\/span> <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">)<\/span> <span class=\"p\">[<\/span><span class=\"n\">mtk_view<\/span> <span class=\"n\">depthStencilTexture<\/span><span class=\"p\">],<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">msaa_color_texture<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">__bridge<\/span> <span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">)<\/span> <span class=\"p\">[<\/span><span class=\"n\">mtk_view<\/span> <span class=\"n\">multisampleColorTexture<\/span><span class=\"p\">],<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Also check out the Metal window system glue code here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/metal\/osxentry.m\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/metal\/osxentry.m<\/a><\/p>\n\n<p>\u2026alternatively check out the GLFW+Metal example here which doesn\u2019t use an MTKView (but also doesn\u2019t\nsupport a depth-buffer or MSAA rendering):<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/metal-glfw.m\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/metal-glfw.m<\/a><\/p>\n\n<h4 id=\"using-webgpu\">\u2026using WebGPU<\/h4>\n\n<p>The environment- and swapchain-helper-functions look very similar to D3D11:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_environment<\/span> <span class=\"nf\">wgpu_environment<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_environment<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">defaults<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_<\/span><span class=\"p\">...,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_<\/span><span class=\"p\">...,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">desc<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">wgpu<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">device<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"k\">const<\/span> <span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">)<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">device<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n\n<\/code><\/pre><\/div><\/div>\n\n<p>For <code class=\"language-plaintext highlighter-rouge\">.defaults.color_format<\/code> you should use the result of\n<code class=\"language-plaintext highlighter-rouge\">wgpuSurfaceGetPreferredFormat()<\/code> translated to a sokol-gfx pixel format\n(either <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_BGRA8<\/code> or <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_RGBA8<\/code>).<\/p>\n\n<p>For the depth format use either <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH_STENCIL<\/code>,\n<code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH<\/code> or <code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_NONE<\/code>, which translate to WebGPU\npixel formats as follows:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH_STENCIL<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">WGPUTextureFormat_Depth32FloatStencil8<\/code><\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">SG_PIXELFORMAT_DEPTH<\/code> =&gt; <code class=\"language-plaintext highlighter-rouge\">WGPUTextureFormat_Depth32Float<\/code><\/li>\n<\/ul>\n\n<p>The type of <code class=\"language-plaintext highlighter-rouge\">state.device<\/code> is <code class=\"language-plaintext highlighter-rouge\">WGPUDevice<\/code>.<\/p>\n\n<p>The WebGPU swapchain helper function might look like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_swapchain<\/span> <span class=\"nf\">wgpu_swapchain<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_swapchain<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">width<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">height<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_<\/span><span class=\"p\">...,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_<\/span><span class=\"p\">...,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">wgpu<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">render_view<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">rt_view<\/span> <span class=\"o\">:<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">msaa_view<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">resolve_view<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span> <span class=\"o\">?<\/span> <span class=\"mi\">0<\/span> <span class=\"o\">:<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">rt_view<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_stencil_view<\/span> <span class=\"o\">=<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">ds_view<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026note the selection for <code class=\"language-plaintext highlighter-rouge\">.wgpu.render_view<\/code> and <code class=\"language-plaintext highlighter-rouge\">.wgpu.resolve_view<\/code> based on the MSAA\nsample count, which works the same as in the <code class=\"language-plaintext highlighter-rouge\">d3d11_swapchain()<\/code> function.<\/p>\n\n<p>The types for all view objects are <code class=\"language-plaintext highlighter-rouge\">WGPUTextureView<\/code>.<\/p>\n\n<p>Also check out the WebGPU system glue code here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/wgpu_entry.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/wgpu\/wgpu_entry.c<\/a><\/p>\n\n<h4 id=\"gl-with-glfw\">\u2026GL with GLFW<\/h4>\n\n<p>The environment-helper-function only returns default pixel formats and sample count:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_environment<\/span> <span class=\"nf\">glfw_environment<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_environment<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">defaults<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_RGBA8<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">4<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">},<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026the swapchain function also returns a GL framebuffer object, for the default framebuffer\nthis is always zero, otherwise this is a handle created with <code class=\"language-plaintext highlighter-rouge\">glGenFramebuffers()<\/code>.<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">sg_swapchain<\/span> <span class=\"nf\">glfw_swapchain<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">int<\/span> <span class=\"n\">width<\/span><span class=\"p\">,<\/span> <span class=\"n\">height<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">glfwGetFramebufferSize<\/span><span class=\"p\">(<\/span><span class=\"n\">_window<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">width<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">height<\/span><span class=\"p\">);<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_swapchain<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">width<\/span> <span class=\"o\">=<\/span> <span class=\"n\">width<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">height<\/span> <span class=\"o\">=<\/span> <span class=\"n\">height<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">sample_count<\/span> <span class=\"o\">=<\/span> <span class=\"n\">_sample_count<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">color_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_RGBA8<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">depth_format<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_PIXELFORMAT_DEPTH_STENCIL<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">gl<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">framebuffer<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Also see <a href=\"https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/glfw_glue.c\">https:\/\/github.com\/floooh\/sokol-samples\/blob\/master\/glfw\/glfw_glue.c<\/a><\/p>\n\n<h2 id=\"q-why-still-have-a-baked-pass-attachments-object\">Q: Why still have a baked pass attachments object?<\/h2>\n\n<p>I\u2019ve been pondering for a little bit to get rid of pre-baked pass-attachments\nobjects alltogether (e.g. what were formerly <code class=\"language-plaintext highlighter-rouge\">sg_pass<\/code> objects and are now\n<code class=\"language-plaintext highlighter-rouge\">sg_attachments<\/code> objects), and instead pass a transient struct with the same\ninformation that\u2019s in <code class=\"language-plaintext highlighter-rouge\">sg_attachments_desc<\/code> into the <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code>\nfunction, similar to how <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> takes a transient <code class=\"language-plaintext highlighter-rouge\">sg_bindings<\/code>\nstruct with all the resource bindings.<\/p>\n\n<p>I didn\u2019t follow through with that idea because this would mean creating\ntemporary objects inside <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> and discarding them again in\n<code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code> (or alternatively use a \u2018hash-and-cache\u2019 approach).<\/p>\n\n<p>In D3D11 and WebGPU, one temporary texture view object would need\nto be created per pass-attachment (which may add up to 9 temporary objects),\nand in the GL backend, a GL framebuffer object must be created,\nconfigured and checked for completeness. All this work currently\nonly happens once in <code class=\"language-plaintext highlighter-rouge\">sg_make_attachments()<\/code>, but would need to happen\ninside <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> without baked attachments objects.<\/p>\n\n<p>While these backend API objects should be \u2018reasonably cheap\u2019 to create, I still\ndecided against it.<\/p>\n\n<p>Currently the only other place where such temporary objects are created and\ndiscarded on the fly are in the <code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code> call for the WebGPU\nbackend, where temporary BindGroup objects are created and discarded\ndynamically via a \u2018hash-and-cache\u2019 approach and I hate it :) I don\u2019t want that\ntype of code to creep into other places.<\/p>\n\n<p>Now, <code class=\"language-plaintext highlighter-rouge\">sg_begin_pass()<\/code> and <code class=\"language-plaintext highlighter-rouge\">sg_end_pass()<\/code> are by far not as high-frequency-calls as\n<code class=\"language-plaintext highlighter-rouge\">sg_apply_bindings()<\/code>, and creating view- and framebuffer-objects <em>should<\/em> be\ncheap enough, but it still feels \u2018wrong\u2019 to create and discard backend API\nobjects willy-nilly during the frame.<\/p>\n","pubDate":"Mon, 26 Feb 2024 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2024\/02\/26\/sokol-spring-cleaning-2024.html","guid":"https:\/\/floooh.github.io\/2024\/02\/26\/sokol-spring-cleaning-2024.html"},{"title":"VSCode, WASM, WASI","description":"<p>I did a neat little thing during my year-end vacation: A VSCode extension for retro-assembly coding with the assembler\nand home computer emulator integrated right into VSCode via WASM and WASI.<\/p>\n\n<p>The extension is here (careful: it must be installed as <strong>pre-release<\/strong>, otherwise installing a dependency extension\nwon\u2019t work, more on that later):<\/p>\n\n<p><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=floooh.vscode-kcide\">https:\/\/marketplace.visualstudio.com\/items?itemName=floooh.vscode-kcide<\/a><\/p>\n\n<p>This is what it looks like in action when debugging a KC85\/4 demo I wrote for dog-fooding the extension:<\/p>\n\n<p><img src=\"\/images\/vscode-wasm-wasi-1.webp\" alt=\"Screenshot 1\" \/><\/p>\n\n<p>The VSCode extension project is here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/vscode-kcide\">https:\/\/github.com\/floooh\/vscode-kcide<\/a><\/p>\n\n<p>\u2026and the samples for KC85\/4, C64 and Amstrad CPC are here:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/kcide-sample\">https:\/\/github.com\/floooh\/kcide-sample<\/a><\/p>\n\n<p>The extension also integrates the following projects:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/floooh\/easmx\">a fork<\/a> of the <a href=\"http:\/\/svn.xi6.com\/svn\/asmx\/branches\/2.x\/asmx-doc.html\">ASMX multi-cpu assembler<\/a><\/li>\n  <li>the KC85\/4, C64 and CPC emulators from my <a href=\"https:\/\/floooh.github.io\/tiny8bit\/\">chips project<\/a><\/li>\n<\/ul>\n\n<p>Creating a simple VSCode extension is fairly straightforward (see: <a href=\"https:\/\/code.visualstudio.com\/api\/get-started\/your-first-extension\">Your First Extension<\/a>),\nso I won\u2019t go into too many details there. What\u2019s interesting is the use of WASM and WASI to integrate projects written\nin other languages than JS\/TS into a VSCode extension.<\/p>\n\n<p>This allows to bundle the assembler (written in C89) and the emulator (C99 and C++11) directly with the\nextension as WASM blobs. Similar extensions without WASM components\nwould either need to port the assembler and emulator to JS\/TS, ask the user to install and run native tools\n(most other retro-dev extensions seem to use that approach), or automatically download and install separate platform-specific native tools\n(the approach used by the Microsoft C\/C++ extension), which is asking for a lot of trust from the extension user.<\/p>\n\n<p>WASM fixes all those issues:<\/p>\n\n<ul>\n  <li>it\u2019s completely hassle-free for the user because the WASM blobs can be bundled with the extension and everything works out of the box<\/li>\n  <li>it\u2019s less hassle for the extension developer, because a single WASM blob automatically works on all platforms supported by VSCode (including the VSCode web version)<\/li>\n  <li>\u2026and unlike native binaries, WASM and WASI don\u2019t add any more security concerns over regular VSCode extensions written in TS\/JS<\/li>\n<\/ul>\n\n<p>Also, how cool is it that I can take an assembler written in C89 in the 90\u2019s and safely run that without\ncode changes in the VSCode web version?<\/p>\n\n<p>(I <strong>did<\/strong> actually consider writing my own assembler in Typescript a long time ago just for the purpose of running\nit in VSCode but quickly abandondend that idea, here are the ruins of that folly: <a href=\"https:\/\/github.com\/floooh\/hcasm\">https:\/\/github.com\/floooh\/hcasm<\/a>)<\/p>\n\n<h2 id=\"paths-not-taken\">Paths not taken<\/h2>\n\n<p>I considered various approaches:<\/p>\n\n<ol>\n  <li>a native IDE via Qt similar to Goran Devic\u2019s <a href=\"https:\/\/baltazarstudios.com\/z80explorer\/\">Z80 Explorer<\/a><\/li>\n  <li>integrate the IDE features right into the emulator via <a href=\"https:\/\/github.com\/ocornut\/imgui\">Dear ImGui<\/a> (the emulators already have an extensive Dear ImGui debugging UI)<\/li>\n  <li>create a VSCode extension which calls into an assembler and emulator written in Typescript<\/li>\n  <li>create a VSCode extension which calls into native assembler and emulator binaries<\/li>\n  <li>create a VSCode extension which uses WASM for the assembler and emulator<\/li>\n<\/ol>\n\n<p>The final decision to use VSCode with WASM comes down to a couple of central problems:<\/p>\n\n<ul>\n  <li>dealing with native tools in a cross-platform scenario is a massive PITA these days:\n    <ul>\n      <li>running the same binary across different Linux distros is still pretty much an unsolved problem<\/li>\n      <li>on Windows and macOS you\u2019ll get all sorts of scare popups when trying to run an executable downloaded from the internet<\/li>\n    <\/ul>\n  <\/li>\n  <li>porting a code base to TS\/JS just so that it can be hooked up into a VSCode extension is almost always a massive waste of time<\/li>\n<\/ul>\n\n<p>In the end it was a decision between (2: extend the existing Dear ImGui emulator UI with IDE features), and\n(4: figure out how to integrate the assembler and emulator as WASM blobs into a VSCode extension).<\/p>\n\n<p>While I enjoy writing Dear ImGui UIs immensely, a robust text editing experience which can rival a dedicated text editor like VSCode would be\na massive project on its own.<\/p>\n\n<p>\u2026which leaves (4) as the one option which enables the most robust result for the least amount of work (important, since this is\na \u2018vacation side project\u2019 which shouldn\u2019t increase my spare time software maintenance burden even more).<\/p>\n\n<p>All in all the extension was finished in about 3 weeks of focused work (spread over 6 real-world weeks, with 2 weeks spent dog-fooding on a little <a href=\"https:\/\/floooh.github.io\/kcide-sample\/kc854.html?file=demo.kcc\">KC85\/4 assembly demo<\/a>).<\/p>\n\n<p>Of the 3 weeks working on the VSCode extension, about 2 weeks were spent on the Debug Adapter alone\n(a lot more effort than I initially expected).<\/p>\n\n<h2 id=\"the-boring-parts\">The boring parts<\/h2>\n\n<p>I\u2019ll run very quickly over the parts of the extension that are not all that interesting (since all of that\nis just reading the <a href=\"https:\/\/code.visualstudio.com\/api\">VSCode extension documentation<\/a> about what features\ncan be provided by extensions and how to implement them).<\/p>\n\n<p>The KC IDE extension implements:<\/p>\n\n<ul>\n  <li>a handful of <strong>Commands<\/strong> which can be invoked via the <code class=\"language-plaintext highlighter-rouge\">Ctrl-P<\/code> command palette:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">KCIDE: Build<\/code>: assembles the source code into a binary file compatible with the\ncurrent emulator<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">KCIDE: Debug<\/code>: builds the source and starts a debugging session<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">KCIDE: Open Emulator<\/code>: (re-)opens the emulator tab<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">KCIDE: Reboot Emulator<\/code>: cold-boots the emulator and stops active debug session<\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">KCIDE: Reset Emulator<\/code>: resets the emulator and stops active debug session (on some home computers,\na reset preserves the memory content)<\/li>\n    <\/ul>\n  <\/li>\n  <li>two <strong>Key Bindings<\/strong>: <code class=\"language-plaintext highlighter-rouge\">F5<\/code> to start a debug session and <code class=\"language-plaintext highlighter-rouge\">F7<\/code> to build the project source code into a binary file<\/li>\n  <li>a <strong>JSON Schema<\/strong> for a <code class=\"language-plaintext highlighter-rouge\">kcide.project.json<\/code> file which defines the target\ncomputer system, assembly dialect, file paths and output binary file format loadable by the emulator<\/li>\n  <li>a <strong>Language Grammar<\/strong> for regex-based syntax highlighting (Z80 and 6502 assembly statements, plus ASMX-specific keywords)<\/li>\n  <li>a <strong>Debug Adapter<\/strong> to connect the VSCode debugging UI with the (already existing) debugger that\u2019s integrated into the emulator<\/li>\n<\/ul>\n\n<p>Some notable VSCode extension features which are <strong>not<\/strong> implemented:<\/p>\n\n<ul>\n  <li>No <strong>Language Server<\/strong> (to provide error squiggles and code completion while typing), the LSP protocol\nis a bit of overkill for low level languages like assembly, while it would have been a \u2018nice to have\u2019\nfeature, it wasn\u2019t doable in the available time, and features similar to a full LSP can most likely\nalso be implemented without a full LSP implementation (VSCode has a couple of other language features\nlike <a href=\"https:\/\/code.visualstudio.com\/api\/language-extensions\/semantic-highlight-guide\">semantic highlighting<\/a>,\n<a href=\"https:\/\/code.visualstudio.com\/api\/language-extensions\/snippet-guide\">snippets<\/a> or\n<a href=\"https:\/\/code.visualstudio.com\/api\/language-extensions\/programmatic-language-features\">programmatic language features<\/a>).\nIn the end I simply ran out of time, maybe in the next round of updates\u2026<\/li>\n  <li>No <strong>Task Providers<\/strong> (e.g. proper integration with <code class=\"language-plaintext highlighter-rouge\">tasks.json<\/code> and <code class=\"language-plaintext highlighter-rouge\">launch.json<\/code>). This also seemed\nlike overkill. Just adding two key bindings while the extension is active (<code class=\"language-plaintext highlighter-rouge\">F5<\/code> for debugging and <code class=\"language-plaintext highlighter-rouge\">F7<\/code> for building)\nachieves the same thing with less hassle for the user.<\/li>\n<\/ul>\n\n<p>Finally, a VSCode extension may run in 3 environments, which has some subtle consequences for what APIs can be\nused in the extension code:<\/p>\n\n<ul>\n  <li><strong>desktop<\/strong>: the extension only works in \u2018desktop VSCode\u2019 and can use the full set of node.js APIs<\/li>\n  <li><strong>web<\/strong>: the extension works in \u2018VSCode for the web\u2019, which means only the VSCode extension API and browser APIs can be called<\/li>\n  <li><strong>universal<\/strong>: the extension can run both in desktop and web VSCode<\/li>\n<\/ul>\n\n<p>The KC IDE is a universal extension, but still has some issues when running in the web version of VSCode (which comes down to a mix\nof VSCode issues and some file-IO related issues I will most likely need to fix on my side).<\/p>\n\n<h2 id=\"integrating-the-assembler-via-wasi\">Integrating the assembler via WASI<\/h2>\n\n<p>This turned out a lot easier than expected, because the <a href=\"https:\/\/code.visualstudio.com\/blogs\/2023\/06\/05\/vscode-wasm-wasi\">VSCode WASI extension<\/a>\ndoes all the hard work.<\/p>\n\n<p>What this extension basically does is to allow any POSIX commandline tool to run inside VSCode without requiring\nchanges to the source (most notably, no changes are required for blocking file IO code via fopen\/fread\/fwrite\/fclose).<\/p>\n\n<p>The only thing I had to fix in the ASMX assembler was a separately provided root path for the assembler\u2019s\n<code class=\"language-plaintext highlighter-rouge\">include<\/code> statement (which is supposed to work with relative paths). WASI currently doesn\u2019t have the concept\nof a \u2018current working directory\u2019, so all filesystem paths must be resolved to absolute paths within the\nWASI container\u2019s virtual filesystem (a WASI environment doesn\u2019t use direct filesystem paths of the host system,\nbut instead defines its own virtual filesystem with mount points mapped to host system directories).<\/p>\n\n<p>The basic procedure to get the assembler working inside VSCode is:<\/p>\n\n<ul>\n  <li>compile the assembler to a WASI blob using the <a href=\"https:\/\/github.com\/WebAssembly\/wasi-sdk\">WASI SDK Clang toolchain<\/a>,\nthis happens manually outside the extension project, the resulting .wasm blob is then simply committed into the\nextension\u2019s git repo and bundled with the published extension. The size of the WASM blob is about 200 KBytes.<\/li>\n  <li>\n    <p>in the VSCode extension code: initialize the WASI runtime, setup a virtual filesystem, and load and compile the\nassembler WASM blob, this happens only once during the extension\u2019s life cycle:<\/p>\n\n    <div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">export<\/span> <span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">requireWasiEnv<\/span><span class=\"p\">(<\/span><span class=\"nx\">ext<\/span><span class=\"p\">:<\/span> <span class=\"nx\">ExtensionContext<\/span><span class=\"p\">):<\/span> <span class=\"nb\">Promise<\/span><span class=\"o\">&lt;<\/span><span class=\"nx\">WasiEnv<\/span><span class=\"o\">&gt;<\/span> <span class=\"p\">{<\/span>\n      <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">wasiEnv<\/span> <span class=\"o\">===<\/span> <span class=\"kc\">null<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n          <span class=\"kd\">const<\/span> <span class=\"nx\">wasm<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">Wasm<\/span><span class=\"p\">.<\/span><span class=\"nx\">load<\/span><span class=\"p\">();<\/span>\n          <span class=\"kd\">const<\/span> <span class=\"nx\">fs<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">wasm<\/span><span class=\"p\">.<\/span><span class=\"nx\">createRootFileSystem<\/span><span class=\"p\">([<\/span> <span class=\"p\">{<\/span> <span class=\"na\">kind<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">workspaceFolder<\/span><span class=\"dl\">'<\/span> <span class=\"p\">}<\/span> <span class=\"p\">]);<\/span>\n          <span class=\"kd\">const<\/span> <span class=\"nx\">bits<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">workspace<\/span><span class=\"p\">.<\/span><span class=\"nx\">fs<\/span><span class=\"p\">.<\/span><span class=\"nx\">readFile<\/span><span class=\"p\">(<\/span><span class=\"nx\">Uri<\/span><span class=\"p\">.<\/span><span class=\"nx\">joinPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">ext<\/span><span class=\"p\">.<\/span><span class=\"nx\">extensionUri<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">media\/asmx.wasm<\/span><span class=\"dl\">'<\/span><span class=\"p\">));<\/span>\n          <span class=\"kd\">const<\/span> <span class=\"nx\">asmx<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">WebAssembly<\/span><span class=\"p\">.<\/span><span class=\"nx\">compile<\/span><span class=\"p\">(<\/span><span class=\"nx\">bits<\/span><span class=\"p\">);<\/span>\n          <span class=\"nx\">wasiEnv<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">wasm<\/span><span class=\"p\">,<\/span> <span class=\"nx\">fs<\/span><span class=\"p\">,<\/span> <span class=\"nx\">asmx<\/span> <span class=\"p\">};<\/span>\n      <span class=\"p\">}<\/span>\n      <span class=\"k\">return<\/span> <span class=\"nx\">wasiEnv<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>\n    <p>run the assembler WASM blob, capture stdout and stderr and check the exit code, this is quite similar to how a native tool would be launched:<\/p>\n\n    <div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">export<\/span> <span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">runAsmx<\/span><span class=\"p\">(<\/span><span class=\"nx\">ext<\/span><span class=\"p\">:<\/span> <span class=\"nx\">ExtensionContext<\/span><span class=\"p\">,<\/span> <span class=\"nx\">args<\/span><span class=\"p\">:<\/span> <span class=\"kr\">string<\/span><span class=\"p\">[]):<\/span> <span class=\"nb\">Promise<\/span><span class=\"o\">&lt;<\/span><span class=\"nx\">RunAsmxResult<\/span><span class=\"o\">&gt;<\/span> <span class=\"p\">{<\/span>\n      <span class=\"kd\">const<\/span> <span class=\"nx\">wasiEnv<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">requireWasiEnv<\/span><span class=\"p\">(<\/span><span class=\"nx\">ext<\/span><span class=\"p\">);<\/span>\n      <span class=\"kd\">const<\/span> <span class=\"nx\">process<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">wasiEnv<\/span><span class=\"p\">.<\/span><span class=\"nx\">wasm<\/span><span class=\"p\">.<\/span><span class=\"nx\">createProcess<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">asmx<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">wasiEnv<\/span><span class=\"p\">.<\/span><span class=\"nx\">asmx<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span>\n          <span class=\"na\">rootFileSystem<\/span><span class=\"p\">:<\/span> <span class=\"nx\">wasiEnv<\/span><span class=\"p\">.<\/span><span class=\"nx\">fs<\/span><span class=\"p\">,<\/span>\n          <span class=\"na\">stdio<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n              <span class=\"na\">out<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"na\">kind<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">pipeOut<\/span><span class=\"dl\">'<\/span> <span class=\"p\">},<\/span>\n              <span class=\"na\">err<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"na\">kind<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">pipeOut<\/span><span class=\"dl\">'<\/span> <span class=\"p\">},<\/span>\n          <span class=\"p\">},<\/span>\n          <span class=\"nx\">args<\/span><span class=\"p\">,<\/span>\n      <span class=\"p\">});<\/span>\n      <span class=\"kd\">const<\/span> <span class=\"nx\">decoder<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">TextDecoder<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">utf-8<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n      <span class=\"kd\">let<\/span> <span class=\"nx\">stderr<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">''<\/span><span class=\"p\">;<\/span>\n      <span class=\"kd\">let<\/span> <span class=\"nx\">stdout<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">''<\/span><span class=\"p\">;<\/span>\n      <span class=\"nx\">process<\/span><span class=\"p\">.<\/span><span class=\"nx\">stderr<\/span><span class=\"o\">!<\/span><span class=\"p\">.<\/span><span class=\"nx\">onData<\/span><span class=\"p\">((<\/span><span class=\"nx\">data<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n          <span class=\"nx\">stderr<\/span> <span class=\"o\">+=<\/span> <span class=\"nx\">decoder<\/span><span class=\"p\">.<\/span><span class=\"nx\">decode<\/span><span class=\"p\">(<\/span><span class=\"nx\">data<\/span><span class=\"p\">);<\/span>\n      <span class=\"p\">});<\/span>\n      <span class=\"nx\">process<\/span><span class=\"p\">.<\/span><span class=\"nx\">stdout<\/span><span class=\"o\">!<\/span><span class=\"p\">.<\/span><span class=\"nx\">onData<\/span><span class=\"p\">((<\/span><span class=\"nx\">data<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n          <span class=\"nx\">stdout<\/span> <span class=\"o\">+=<\/span> <span class=\"nx\">decoder<\/span><span class=\"p\">.<\/span><span class=\"nx\">decode<\/span><span class=\"p\">(<\/span><span class=\"nx\">data<\/span><span class=\"p\">);<\/span>\n      <span class=\"p\">});<\/span>\n      <span class=\"kd\">const<\/span> <span class=\"nx\">exitCode<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">process<\/span><span class=\"p\">.<\/span><span class=\"nx\">run<\/span><span class=\"p\">();<\/span>\n      <span class=\"k\">return<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">exitCode<\/span><span class=\"p\">,<\/span> <span class=\"nx\">stdout<\/span><span class=\"p\">,<\/span> <span class=\"nx\">stderr<\/span> <span class=\"p\">};<\/span>\n  <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div>    <\/div>\n  <\/li>\n  <li>the KC IDE extension will then parse the assembler error messages in stderr and convert the error messages into VSCode Diagnostic objects,\nwhich then show up in the <code class=\"language-plaintext highlighter-rouge\">Problems<\/code> panel and as error squiggles in the text editor<\/li>\n  <li>the actual assembler output files are written directly into the host filesystem via the virtual filesystem mapping that was provided\nwhen initializing the WASI runtime<\/li>\n<\/ul>\n\n<h2 id=\"integrating-the-emulator\">Integrating the emulator<\/h2>\n\n<p>The embedded home computer emulators are taken from the <a href=\"https:\/\/github.com\/floooh\/chips\">chips project<\/a>,\nthose are implemented in C\/C++, use the <a href=\"https:\/\/github.com\/floooh\/sokol\">sokol headers<\/a> for abstracting platform details\nand run both as natively compiled executables and <a href=\"https:\/\/floooh.github.io\/tiny8bit\/\">in the browser<\/a> via WASM and WebGL, compiled\nwith the Emscripten SDK.<\/p>\n\n<p>One emulator WASM blob is about 700..800 KBytes (most of that is the Dear ImGui debugging UI which costs about 450 Kbytes).<\/p>\n\n<p>Currently the KC IDE extension contains 4 emulators (KC85\/3, KC85\/4, C64 and CPC) which adds up to about 3 MBytes (if there will\nbe drastically more supported systems in the future I\u2019ll need to come up with a solution to reduce the size of the embedded\nemulators, either downloading them on demand, merge them into a single \u2018multi-system-emulator\u2019 binary, or maybe moving the UI into a shared\nWASM module that\u2019s loaded like a DLL).<\/p>\n\n<p>The emulator is running inside a VSCode <a href=\"https:\/\/code.visualstudio.com\/api\/extension-guides\/webview\">webview panel<\/a>. For the\nmost part this is quite straightforward for an Emscripten WebGL application by taking an <a href=\"https:\/\/github.com\/floooh\/vscode-kcide\/blob\/b062aa56609fafeffc70ef0ac440c6ee1d70fe5b\/media\/shell.html\">index.html like this<\/a> (note the placeholders <code class=\"language-plaintext highlighter-rouge\">{{{shell}}}<\/code> and <code class=\"language-plaintext highlighter-rouge\">{{{emu}}}<\/code>, those must be replaced with runtime-generated URLs), and setup\na <a href=\"https:\/\/github.com\/floooh\/vscode-kcide\/blob\/b062aa56609fafeffc70ef0ac440c6ee1d70fe5b\/src\/emu.ts#L22-L77\">webview panel object like this<\/a>.<\/p>\n\n<p>There\u2019s a couple of interesting details in that code:<\/p>\n\n<p>The webview panel cannot simply load resources from anywhere in the host file system, instead a <code class=\"language-plaintext highlighter-rouge\">localResourceRoot<\/code> must be provided\nin the <code class=\"language-plaintext highlighter-rouge\">window.createWebviewPanel()<\/code> call which points to the extension subdirectory <code class=\"language-plaintext highlighter-rouge\">media\/<\/code> (e.g. anything that\u2019s loaded in the\nwebview panel needs to be located in that <code class=\"language-plaintext highlighter-rouge\">media\/<\/code> subdirectory):<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"kd\">const<\/span> <span class=\"nx\">rootUri<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">Uri<\/span><span class=\"p\">.<\/span><span class=\"nx\">joinPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">getExtensionUri<\/span><span class=\"p\">(),<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">media<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">panel<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nx\">createWebviewPanel<\/span><span class=\"p\">(<\/span>\n      <span class=\"c1\">\/\/ ...<\/span>\n      <span class=\"p\">{<\/span>\n        <span class=\"na\">localResourceRoots<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span> <span class=\"nx\">rootUri<\/span> <span class=\"p\">],<\/span>\n      <span class=\"p\">}<\/span>\n    <span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026next, all URLs referenced in the webview panel\u2019s HTML content must be generated via the webview panel API, I\u2019m doing that by loading a HTML template file and then replace the placeholders inside <code class=\"language-plaintext highlighter-rouge\">{{{...}}}<\/code> with generated URLs (and while\nat it, I also select the correct emulator to load):<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"kd\">let<\/span> <span class=\"nx\">emuFilename<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">switch<\/span> <span class=\"p\">(<\/span><span class=\"nx\">project<\/span><span class=\"p\">.<\/span><span class=\"nx\">emulator<\/span><span class=\"p\">.<\/span><span class=\"nx\">system<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"k\">case<\/span> <span class=\"nx\">System<\/span><span class=\"p\">.<\/span><span class=\"nx\">KC853<\/span><span class=\"p\">:<\/span>      <span class=\"nx\">emuFilename<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">kc853-ui.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n        <span class=\"k\">case<\/span> <span class=\"nx\">System<\/span><span class=\"p\">.<\/span><span class=\"nx\">C64<\/span><span class=\"p\">:<\/span>        <span class=\"nx\">emuFilename<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">c64-ui.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n        <span class=\"k\">case<\/span> <span class=\"nx\">System<\/span><span class=\"p\">.<\/span><span class=\"nx\">CPC6128<\/span><span class=\"p\">:<\/span>    <span class=\"nx\">emuFilename<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">cpc-ui.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n        <span class=\"nl\">default<\/span><span class=\"p\">:<\/span>                <span class=\"nx\">emuFilename<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">kc854-ui.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">emuUri<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">panel<\/span><span class=\"p\">.<\/span><span class=\"nx\">webview<\/span><span class=\"p\">.<\/span><span class=\"nx\">asWebviewUri<\/span><span class=\"p\">(<\/span><span class=\"nx\">Uri<\/span><span class=\"p\">.<\/span><span class=\"nx\">joinPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">rootUri<\/span><span class=\"p\">,<\/span> <span class=\"nx\">emuFilename<\/span><span class=\"p\">));<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">shellUri<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">panel<\/span><span class=\"p\">.<\/span><span class=\"nx\">webview<\/span><span class=\"p\">.<\/span><span class=\"nx\">asWebviewUri<\/span><span class=\"p\">(<\/span><span class=\"nx\">Uri<\/span><span class=\"p\">.<\/span><span class=\"nx\">joinPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">rootUri<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">shell.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">));<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">templ<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">readTextFile<\/span><span class=\"p\">(<\/span><span class=\"nx\">Uri<\/span><span class=\"p\">.<\/span><span class=\"nx\">joinPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">rootUri<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">shell.html<\/span><span class=\"dl\">'<\/span><span class=\"p\">));<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">html<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">templ<\/span><span class=\"p\">.<\/span><span class=\"nx\">replace<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">{{{emu}}}<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">emuUri<\/span><span class=\"p\">.<\/span><span class=\"nx\">toString<\/span><span class=\"p\">()).<\/span><span class=\"nx\">replace<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">{{{shell}}}<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">shellUri<\/span><span class=\"p\">.<\/span><span class=\"nx\">toString<\/span><span class=\"p\">());<\/span>\n    <span class=\"nx\">panel<\/span><span class=\"p\">.<\/span><span class=\"nx\">webview<\/span><span class=\"p\">.<\/span><span class=\"nx\">html<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">html<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Communication between VSCode and the WebView panel content works via bi-directional message passing, this means\nthe VSCode extension needs to register a listener function which dispatches received messages to their handler\nfunctions:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"nx\">panel<\/span><span class=\"p\">.<\/span><span class=\"nx\">webview<\/span><span class=\"p\">.<\/span><span class=\"nx\">onDidReceiveMessage<\/span><span class=\"p\">((<\/span><span class=\"nx\">msg<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">command<\/span> <span class=\"o\">===<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">emu_cpustate<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"nx\">cpuStateResolved<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">state<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">CPUState<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">command<\/span> <span class=\"o\">===<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">emu_disassembly<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"nx\">disassemblyResolved<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">result<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">DisasmLine<\/span><span class=\"p\">[]);<\/span>\n        <span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">command<\/span> <span class=\"o\">===<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">emu_memory<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"nx\">readMemoryResolved<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">result<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">ReadMemoryResult<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">command<\/span> <span class=\"o\">===<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">emu_ready<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">state<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n                <span class=\"nx\">state<\/span><span class=\"p\">.<\/span><span class=\"nx\">ready<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">isReady<\/span><span class=\"p\">;<\/span>\n            <span class=\"p\">}<\/span>\n        <span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span>\n            <span class=\"nx\">KCIDEDebugSession<\/span><span class=\"p\">.<\/span><span class=\"nx\">onEmulatorMessage<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026sending a message into the opposite direction (from the debug session to the webview panel) simply\nlooks like this:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>  <span class=\"k\">await<\/span> <span class=\"nx\">state<\/span><span class=\"p\">.<\/span><span class=\"nx\">panel<\/span><span class=\"p\">.<\/span><span class=\"nx\">webview<\/span><span class=\"p\">.<\/span><span class=\"nx\">postMessage<\/span><span class=\"p\">({<\/span> <span class=\"na\">cmd<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boot<\/span><span class=\"dl\">'<\/span> <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026the message structure is entirely custom (and I\u2019m just noticing that I\u2019m using <code class=\"language-plaintext highlighter-rouge\">command<\/code> in one direction,\nbut <code class=\"language-plaintext highlighter-rouge\">cmd<\/code> in the other direction\u2026 but anyway\u2026).<\/p>\n\n<p>There is one missing step in the communication between VSCode debug session on one side, and the emulator on the other. There\u2019s a <a href=\"https:\/\/github.com\/floooh\/vscode-kcide\/blob\/main\/media\/shell.js\">Javascript shim<\/a>\nrunning in the context of the webpage which translates between the JSON-like message objects which are sent and received\nby the VSCode debug session, and a lower level WASM function call interface implemented by the emulator.<\/p>\n\n<p>When a message is received from the VSCode debug session in the emulator\u2019s HTML page, it\u2019s dispatched to a Javascript function via\nan event listener added to the <code class=\"language-plaintext highlighter-rouge\">window<\/code> object (note that this code is plain Javascript, not Typescript):<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">message<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">ev<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n        <span class=\"kd\">const<\/span> <span class=\"nx\">msg<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">ev<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">;<\/span>\n        <span class=\"k\">switch<\/span> <span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">cmd<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boot<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_boot<\/span><span class=\"p\">();<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">reset<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_reset<\/span><span class=\"p\">();<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">ready<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_ready<\/span><span class=\"p\">();<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">load<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_load<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">);<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"c1\">\/\/ ...<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">disassemble<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_dbgDisassemble<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">addr<\/span><span class=\"p\">,<\/span> <span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">offsetLines<\/span><span class=\"p\">,<\/span> <span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">numLines<\/span><span class=\"p\">);<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">case<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">readMemory<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"nx\">kcide_dbgReadMemory<\/span><span class=\"p\">(<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">addr<\/span><span class=\"p\">,<\/span> <span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">numBytes<\/span><span class=\"p\">);<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n            <span class=\"nl\">default<\/span><span class=\"p\">:<\/span> <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"s2\">`unknown cmd called: <\/span><span class=\"p\">${<\/span><span class=\"nx\">msg<\/span><span class=\"p\">.<\/span><span class=\"nx\">cmd<\/span><span class=\"p\">}<\/span><span class=\"s2\">`<\/span><span class=\"p\">);<\/span> <span class=\"k\">break<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Such a handler function looks like this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function<\/span> <span class=\"nx\">kcide_boot<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">Module<\/span><span class=\"p\">.<\/span><span class=\"nx\">_webapi_boot<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is an \u2018Emscripten-ism\u2019. The easiest way to export a C function from WASM to Javascript is via the\n<code class=\"language-plaintext highlighter-rouge\">EMSCRIPTEN_KEEPALIVE<\/code> attribute in the C source, like this:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">EMSCRIPTEN_KEEPALIVE<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">webapi_boot<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">inited<\/span> <span class=\"o\">&amp;&amp;<\/span> <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">funcs<\/span><span class=\"p\">.<\/span><span class=\"n\">boot<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"n\">state<\/span><span class=\"p\">.<\/span><span class=\"n\">funcs<\/span><span class=\"p\">.<\/span><span class=\"n\">boot<\/span><span class=\"p\">();<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>When Emscripten builds the project, it keeps track of all <code class=\"language-plaintext highlighter-rouge\">EMSCRIPTEN_KEEPALIVE<\/code> C functions and makes them\navailable as Javascript functions on a global <code class=\"language-plaintext highlighter-rouge\">Module<\/code> object created by the Emscripten entry stub. Calling\nsuch an <code class=\"language-plaintext highlighter-rouge\">EMSCRIPTEN_KEEPALIVE<\/code> C function from the Javascript side then looks like this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"nx\">Module<\/span><span class=\"p\">.<\/span><span class=\"nx\">_webapi_boot<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and that\u2019s essentially how the communication between VSCode and the WASM emulator works. For instance, when the VSCode palette command\n<code class=\"language-plaintext highlighter-rouge\">KCIDE: Reboot Emulator<\/code> is executed, eventually the C function <code class=\"language-plaintext highlighter-rouge\">webapi_boot()<\/code> in the WASM emulator will be\ncalled, which reboots the emulator.<\/p>\n\n<p>Currently the emulators implement the following \u2018web API\u2019 functions callable from Javascript:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_connect<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>          <span class=\"c1\">\/\/ a VSCode debug session has started<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_disconnect<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>       <span class=\"c1\">\/\/ a VSCode debug session has ended<\/span>\n<span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"nf\">webapi_alloc<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span> <span class=\"n\">size<\/span><span class=\"p\">);<\/span>           <span class=\"c1\">\/\/ helper function to allocate on the WASM heap from Javascript<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_free<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"o\">*<\/span><span class=\"p\">);<\/span>                <span class=\"c1\">\/\/ helper function to free memory allocated via webapi_alloc()<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_boot<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                 <span class=\"c1\">\/\/ reboot the emulator (e.g. switch off and on)<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_reset<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                <span class=\"c1\">\/\/ reset the emulator (e.g. press the reset button)<\/span>\n<span class=\"n\">bool<\/span> <span class=\"nf\">webapi_ready<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                <span class=\"c1\">\/\/ returns true when the emulator is ready to start a debug session after rebooting<\/span>\n<span class=\"n\">bool<\/span> <span class=\"nf\">webapi_load<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"o\">*<\/span> <span class=\"n\">ptr<\/span><span class=\"p\">,<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">size<\/span><span class=\"p\">);<\/span>  <span class=\"c1\">\/\/ load binary data into the emulator<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_add_breakpoint<\/span><span class=\"p\">(<\/span><span class=\"kt\">uint16_t<\/span> <span class=\"n\">addr<\/span><span class=\"p\">);<\/span>    <span class=\"c1\">\/\/ add a debug breakpoint at a 16-bit address<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_remove_breakpoint<\/span><span class=\"p\">(<\/span><span class=\"kt\">uint16_t<\/span> <span class=\"n\">addr<\/span><span class=\"p\">);<\/span> <span class=\"c1\">\/\/ delete a debug breakpoint at a 16-bit address<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_break<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>            <span class=\"c1\">\/\/ break into the debugger<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_continue<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>         <span class=\"c1\">\/\/ continue execution when stopped in debugger<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_step_next<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>        <span class=\"c1\">\/\/ execute a 'step over' in the debugger<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_dbg_step_into<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>        <span class=\"c1\">\/\/ execute a 'step into' in the debugger<\/span>\n<span class=\"kt\">uint16_t<\/span><span class=\"o\">*<\/span> <span class=\"nf\">webapi_dbg_cpu_state<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>   <span class=\"c1\">\/\/ request a raw 'CPU state' dump (current register values)<\/span>\n<span class=\"n\">webapi_dasm_line_t<\/span><span class=\"o\">*<\/span> <span class=\"nf\">webapi_dbg_request_disassembly<\/span><span class=\"p\">(<\/span><span class=\"cm\">\/*...*\/<\/span><span class=\"p\">);<\/span> <span class=\"c1\">\/\/ request a disassembly dump over a range of addresses<\/span>\n<span class=\"kt\">uint8_t<\/span><span class=\"o\">*<\/span> <span class=\"nf\">webapi_dbg_read_memory<\/span><span class=\"p\">(<\/span><span class=\"kt\">uint16_t<\/span> <span class=\"n\">addr<\/span><span class=\"p\">,<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">num_bytes<\/span><span class=\"p\">);<\/span> <span class=\"c1\">\/\/ request a memory dump over a range of addresses<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In the opposite direction (from the emulator to the VSCode debug session), the emulator calls into\nthe following C callback functions, which in turn call into Javascript to create a JSON-like message\nobject to send back into the VSCode debug session:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">webapi_event_stopped<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span> <span class=\"n\">stop_reason<\/span><span class=\"p\">,<\/span> <span class=\"kt\">uint16_t<\/span> <span class=\"n\">addr<\/span><span class=\"p\">);<\/span>    <span class=\"c1\">\/\/ debugger has stopped at addr for a specific reason<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_event_continued<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                            <span class=\"c1\">\/\/ the debugger has continued execution<\/span>\n<span class=\"kt\">void<\/span> <span class=\"nf\">webapi_event_reboot<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                               <span class=\"c1\">\/\/ the emulator has been rebooted<\/span>\n<span class=\"kt\">void<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">webapi_event_reset<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">);<\/span>                           <span class=\"c1\">\/\/ the emulator has been reset<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026in a nutshell, this is the minimal \u2018virtual machine\u2019 interface required to implement\na somewhat feature-complete VSCode Debug Adapter.<\/p>\n\n<p>One downside of the <a href=\"https:\/\/microsoft.github.io\/debug-adapter-protocol\/\">Debug Adapter Protocol<\/a>\nis that it is clearly designed towards high level languages, and the protocol feature set\nhas little overlap with debugging features that are desired in an emulator virtual machine.<\/p>\n\n<p>But thankfully, the Debug Adapter Protocol is also flexible enough that it can work side\nby side with the much more powerful debugger that\u2019s already integrated in the chips-emulators\nvia Dear ImGui:<\/p>\n\n<p><img src=\"\/images\/vscode-wasm-wasi-3.webp\" alt=\"Screenshot 3\" \/><\/p>\n\n<p>\u2026for instance, the embedded Dear ImGui debugger allows to step the emulator forward in single\nclock cycles, while the VSCode debugger only steps at instruction or source line granularity.<\/p>\n\n<h2 id=\"known-issues-and-future-updates\">Known Issues and future updates<\/h2>\n\n<p>There\u2019s a couple of issues which are currently worked around or don\u2019t work at all,\nand which I want to fix in future updates (most of those are only an issue in the VSCode\nweb version, so not exactly show stoppers):<\/p>\n\n<ul>\n  <li>\n    <p>Hopefully the <a href=\"https:\/\/github.com\/microsoft\/vscode-wasm\">VSCode WASI extension<\/a> will go out of\npre-release-only mode rather sooner than later, at that point I can also move the KC IDE\nextension out of pre-release. The problem is that trying to install a VSCode extension which depends\non a pre-release-only extension will fail to install the dependency with a cryptic error message. Worst\ncase is that I need to implement my own VSCode WASI runtime, or figure out another way to run the\nassembler inside VSCode (maybe as a regular WASM blob which replaces the C stdlib IO calls with\nasynchronous functions with completion-callback, delegated to Javascript)<\/p>\n  <\/li>\n  <li>\n    <p>Currently, any binary-blob data that needs to be transferred from VSCode into the emulator needs to go through a base64-encoded string\nwhich is expensive to encode and decode. The reason for that hack is that transferring Uint8Array objects doesn\u2019t\nwork when VSCode is running in the web (it\u2019s supposed to work, but the data gets corrupted).<\/p>\n  <\/li>\n  <li>\n    <p>Working directly on Github repositories in the VSCode web version doesn\u2019t work (weird\nvirtual filesystem issues).<\/p>\n  <\/li>\n  <li>\n    <p>\u2026and of course some sort of Language-Server-like editing experience (proper code\ncompletion and error squiggles while typing), but without implementing a full-blown\nlanguage server.<\/p>\n  <\/li>\n<\/ul>\n","pubDate":"Sun, 31 Dec 2023 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2023\/12\/31\/vscode-wasm-wasi.html","guid":"https:\/\/floooh.github.io\/2023\/12\/31\/vscode-wasm-wasi.html"},{"title":"WASM Debugging with Emscripten and VSCode","description":"<p><strong>TL;DR<\/strong>: glueing together VSCode, Cmake and the Emscripten SDK to enable an IDE-like workflow (including debugging).<\/p>\n\n<p><strong>17-Nov-2024<\/strong>: looks like the problem that \u2018early breakpoints\u2019 are not caught is fixed, woohoo!<\/p>\n\n<p><strong>09-Oct-2024<\/strong>: updated for the latest sokol_gfx.h and VSCode extension versions.<\/p>\n\n<p>This is written from the perspective of a UNIX-like OS (macOS or Linux), but should also work on Windows with some minor tweaks.<\/p>\n\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n\n<p>First make sure that the following tools are in the path:<\/p>\n\n<ul>\n  <li>git<\/li>\n  <li>cmake<\/li>\n  <li>ninja<\/li>\n<\/ul>\n\n<p>You\u2019ll also need VSCode and Chrome installed.<\/p>\n\n<p>On macOS I\u2019d recommend using <a href=\"https:\/\/brew.sh\/\">Homebrew<\/a> and on Windows <a href=\"https:\/\/scoop.sh\/\">Scoop<\/a>\nto install those. On Linux of course, your system\u2019s standard package manager.<\/p>\n\n<h2 id=\"emscripten-hello-world\">Emscripten Hello World<\/h2>\n\n<p>Let\u2019s start from scratch. On the command line:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">mkdir <\/span>hello\n<span class=\"nb\">cd <\/span>hello\ngit init\n<\/code><\/pre><\/div><\/div>\n\n<p>Add a <code class=\"language-plaintext highlighter-rouge\">.gitignore<\/code> file:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">.gitignore<\/code><\/p>\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>build\/\nemsdk\/\n<\/code><\/pre><\/div><\/div>\n\n<p>Install the Emscripten SDK, we\u2019ll do so in a way that it doesn\u2019t leave a trace on your system when\ndeleted so don\u2019t worry. Still inside the <code class=\"language-plaintext highlighter-rouge\">hello<\/code> directory:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>git clone --depth=1 https:\/\/github.com\/emscripten-core\/emsdk\ncd emsdk\n.\/emsdk install latest\n.\/emsdk activate --embedded latest\ncd ..\n<\/code><\/pre><\/div><\/div>\n\n<p>Don\u2019t forget the <code class=\"language-plaintext highlighter-rouge\">.\/emsdk activate --embedded latest<\/code> step! (happens to me all the time)<\/p>\n\n<p>\u2026let\u2019s check if that worked. Create a <code class=\"language-plaintext highlighter-rouge\">hello.c<\/code> source file in the <code class=\"language-plaintext highlighter-rouge\">hello<\/code> project directory:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">hello.c<\/code><\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#include<\/span> <span class=\"cpf\">&lt;stdio.h&gt;<\/span><span class=\"cp\">\n<\/span>\n<span class=\"kt\">int<\/span> <span class=\"nf\">main<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">printf<\/span><span class=\"p\">(<\/span><span class=\"s\">\"Hello World!<\/span><span class=\"se\">\\n<\/span><span class=\"s\">\"<\/span><span class=\"p\">);<\/span>\n    <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026compile that into a .wasm\/.js pair runnable with node.js:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>emsdk\/upstream\/emscripten\/emcc hello.c <span class=\"nt\">-o<\/span> hello.js\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026there should be a hello.js and hello.wasm file now:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">ls\n<\/span>emsdk      hello.c    hello.js   hello.wasm\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026run the hello.js file via node.js (depending on the emsdk version the path may differ):<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>emsdk\/node\/18.20.3_64bit\/bin\/node hello.js\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026you should see a <code class=\"language-plaintext highlighter-rouge\">Hello World!<\/code> printed to the terminal.<\/p>\n\n<p>Delete the compiler output, we don\u2019t need that anymore:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">rm <\/span>hello.js hello.wasm\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"cmake--emscripten\">CMake + Emscripten<\/h2>\n\n<p>Let\u2019s bake the build process into a cmake file. Create a CMakeLists.txt\nfile in the <code class=\"language-plaintext highlighter-rouge\">hello<\/code> project directory:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">CMakeLists.txt<\/code><\/p>\n<div class=\"language-cmake highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">cmake_minimum_required<\/span><span class=\"p\">(<\/span>VERSION 3.21<span class=\"p\">)<\/span>\n<span class=\"nb\">project<\/span><span class=\"p\">(<\/span>hello<span class=\"p\">)<\/span>\n<span class=\"nb\">add_executable<\/span><span class=\"p\">(<\/span>hello hello.c<span class=\"p\">)<\/span>\n<span class=\"nb\">if<\/span> <span class=\"p\">(<\/span>CMAKE_SYSTEM_NAME STREQUAL Emscripten<span class=\"p\">)<\/span>\n    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span>CMAKE_EXECUTABLE_SUFFIX .js<span class=\"p\">)<\/span>\n<span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and since this is a cross-compilation scenario, let\u2019s also create\na CMakeUserPresets.json file. This simplifies calling cmake with the\nright arguments for cross-compilation, and will help us later when\nintegrating with VSCode:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">CMakeUserPresets.json<\/code><\/p>\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"mi\">3<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"cmakeMinimumRequired\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n        <\/span><span class=\"nl\">\"major\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"mi\">3<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"nl\">\"minor\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"mi\">21<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"nl\">\"patch\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"mi\">0<\/span><span class=\"w\">\n    <\/span><span class=\"p\">},<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"configurePresets\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"default\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"displayName\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Emscripten\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"binaryDir\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"build\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"generator\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Ninja Multi-Config\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"toolchainFile\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"emsdk\/upstream\/emscripten\/cmake\/Modules\/Platform\/Emscripten.cmake\"<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"buildPresets\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Debug\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"configurePreset\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"default\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"configuration\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Debug\"<\/span><span class=\"w\">\n        <\/span><span class=\"p\">},<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Release\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"configurePreset\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"default\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"configuration\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Release\"<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>\u2026let\u2019s configure and build with cmake:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>cmake <span class=\"nt\">--preset<\/span> default <span class=\"nt\">-B<\/span> build\ncmake <span class=\"nt\">--build<\/span> build <span class=\"nt\">--preset<\/span> Debug\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and run with node.js:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>emsdk\/node\/18.20.3_64bit\/bin\/node build\/Debug\/hello.js\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026this should again print <code class=\"language-plaintext highlighter-rouge\">Hello World!<\/code>.<\/p>\n\n<h2 id=\"vscode--cmake--emscripten\">VSCode + CMake + Emscripten<\/h2>\n\n<p>Let\u2019s integrate what we have so far with VSCode!<\/p>\n\n<p>You\u2019ll need the following VSCode extensions:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.cpptools\">ms-vscode.cpptools<\/a><\/li>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.cmake-tools\">ms-vscode.cmake-tools<\/a><\/li>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.wasm-dwarf-debugging\">ms-vscode.wasm-dwarf-debugging<\/a><\/li>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.live-server\">ms-vscode.live-server<\/a><\/li>\n<\/ul>\n\n<p>\u2026with those installed, start VSCode from within the <code class=\"language-plaintext highlighter-rouge\">hello<\/code> project directory:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>code .\n<\/code><\/pre><\/div><\/div>\n\n<p>You should see something like this, pay attention to the status bar at the bottom (underlined in red), these\nitems are used to control the cmake build config and target:<\/p>\n\n<p>(<strong>NOTE 09-Oct-2024<\/strong>: the underlined items in the bottom bar have moved into the CMake Tools sidepanel in recent versions).<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-1.png\" alt=\"VSCode Screenshot 1\" \/><\/p>\n\n<p>Clicking those allows you to select a Configure- and Build-Preset, and a build target.<\/p>\n\n<p>Change those that it looks like this:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-2.png\" alt=\"VSCode Screenshot 2\" \/><\/p>\n\n<p>Here we also encounter the first wart, the CMake Tools extension isn\u2019t able to communicate the\ncorrect Emscripten sysroot include paths over to the C\/C++ extension. You\u2019ll see an error squiggle\nunder the stdio.h include path:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-3.png\" alt=\"VSCode Screenshot 3\" \/><\/p>\n\n<p>I haven\u2019t found a solution to this problem, it looks like a bug in\nthe CMake Tools extension. Annoying for sure, but not a showstopper, because\nonly Intellisense is affected, building should work fine.<\/p>\n\n<p>You can test that by pressing <code class=\"language-plaintext highlighter-rouge\">F7<\/code>, or run the palette command <code class=\"language-plaintext highlighter-rouge\">CMake: Build<\/code>. You should\nsee something like this in the VSCode Output panel:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>[main] Building folder: hello\n[build] Starting build\n[proc] Executing command: \/opt\/homebrew\/bin\/cmake --build \/Users\/floh\/scratch\/hello\/build --config Debug --target hello\n[build] [1\/2] Building C object CMakeFiles\/hello.dir\/Debug\/hello.c.o\n[build] [2\/2] Linking C executable Debug\/hello.js\n[driver] Build completed: 00:00:00.361\n[build] Build finished with exit code\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"debugging\">Debugging<\/h2>\n\n<p>\u2026next lets make debugging work!<\/p>\n\n<p>Create a <code class=\"language-plaintext highlighter-rouge\">launch.json<\/code> file in the <code class=\"language-plaintext highlighter-rouge\">.vscode<\/code> subdirectory:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">.vscode\/launch.json<\/code><\/p>\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"0.2.0\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"configurations\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Launch\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"node\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"request\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"launch\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"program\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"build\/Debug\/${command:cmake.launchTargetFilename}\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>Pressing <code class=\"language-plaintext highlighter-rouge\">F5<\/code> should now work. You should see a <code class=\"language-plaintext highlighter-rouge\">Hello World!<\/code> in the VSCode <code class=\"language-plaintext highlighter-rouge\">Debug Panel<\/code>.<\/p>\n\n<p>But when trying to debug there\u2019s the next wart. Try to set a breakpoint in the C source code:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-4.png\" alt=\"VSCode Screenshot 4\" \/><\/p>\n\n<p>Now hit <code class=\"language-plaintext highlighter-rouge\">F5<\/code>. We\u2019d expect that the execution stops at the breakpoint, but that doesn\u2019t happen.<\/p>\n\n<p>This is a known issue in the DWARF debugging extension. From the <a href=\"https:\/\/code.visualstudio.com\/docs\/nodejs\/nodejs-debugging#_debugging-webassembly\">documentation<\/a>:<\/p>\n\n<blockquote>\n  <p>Breakpoints in WebAssembly code are resolved asynchronously, so breakpoints hit early on in a program\u2019s lifecycle may be missed. There are plans to fix this in the future. If you\u2019re debugging in a browser, you can refresh the page for your breakpoint to be hit. If you\u2019re in Node.js, you can add an artificial delay, or set another breakpoint, after your WebAssembly module is loaded but before your desired breakpoint is hit.<\/p>\n<\/blockquote>\n\n<p>Hopefully this problem will be fixed soon-ish, since it\u2019s currently the most annoying.<\/p>\n\n<p>One workaround is to first set a breakpoint in the Javascript launch file at a point where the WASM blob has been loaded.<\/p>\n\n<p>Load the file <code class=\"language-plaintext highlighter-rouge\">build\/Debug\/hello.js<\/code> into the editor, search the function <code class=\"language-plaintext highlighter-rouge\">callMain<\/code>, and set a breakpoint there:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-5.png\" alt=\"VSCode Screenshot 5\" \/><\/p>\n\n<p>Press <code class=\"language-plaintext highlighter-rouge\">F5<\/code> and execution should stop at that breakpoint. Now press <code class=\"language-plaintext highlighter-rouge\">F5<\/code> again and execution should stop in the C code\u2019s main() function\n(assuming that breakpoint is still set):<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-6.png\" alt=\"VSCode Screenshot 6\" \/><\/p>\n\n<p>Yay. This is how debugging works for a Node.js Emscripten application.<\/p>\n\n<h3 id=\"moving-into-the-web-browser\">Moving into the web browser<\/h3>\n\n<p>Let\u2019s extend our <code class=\"language-plaintext highlighter-rouge\">hello.c<\/code> to render something in WebGL2.<\/p>\n\n<p>Clone the sokol headers into the <code class=\"language-plaintext highlighter-rouge\">hello<\/code> project directory and copy some headers up into the project:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>git clone <span class=\"nt\">--depth<\/span><span class=\"o\">=<\/span>1 https:\/\/github.com\/floooh\/sokol\n<span class=\"nb\">cp <\/span>sokol\/sokol_gfx.h sokol\/sokol_app.h sokol\/sokol_log.h sokol\/sokol_glue.h <span class=\"nb\">.<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026delete the sokol directory since we don\u2019t need it anymore:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>rm -rf sokol\n<\/code><\/pre><\/div><\/div>\n\n<p>Replace the <code class=\"language-plaintext highlighter-rouge\">hello.c<\/code> file with the following code which just clears the canvas with\na dynamically changing color:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">hello.c<\/code><\/p>\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#define SOKOL_IMPL\n#define SOKOL_GLES3\n#include<\/span> <span class=\"cpf\">\"sokol_gfx.h\"<\/span><span class=\"cp\">\n#include<\/span> <span class=\"cpf\">\"sokol_app.h\"<\/span><span class=\"cp\">\n#include<\/span> <span class=\"cpf\">\"sokol_log.h\"<\/span><span class=\"cp\">\n#include<\/span> <span class=\"cpf\">\"sokol_glue.h\"<\/span><span class=\"cp\">\n<\/span>\n<span class=\"k\">static<\/span> <span class=\"n\">sg_pass_action<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">;<\/span>\n\n<span class=\"k\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">init<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sg_setup<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_desc<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">environment<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sglue_environment<\/span><span class=\"p\">(),<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">logger<\/span><span class=\"p\">.<\/span><span class=\"n\">func<\/span> <span class=\"o\">=<\/span> <span class=\"n\">slog_func<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">});<\/span>\n    <span class=\"n\">pass_action<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">sg_pass_action<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">load_action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">SG_LOADACTION_CLEAR<\/span><span class=\"p\">,<\/span>\n            <span class=\"p\">.<\/span><span class=\"n\">clear_value<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span> <span class=\"p\">}<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">frame<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kt\">float<\/span> <span class=\"n\">g<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">clear_value<\/span><span class=\"p\">.<\/span><span class=\"n\">g<\/span> <span class=\"o\">+<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mo\">01<\/span><span class=\"n\">f<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">pass_action<\/span><span class=\"p\">.<\/span><span class=\"n\">colors<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"n\">clear_value<\/span><span class=\"p\">.<\/span><span class=\"n\">g<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">g<\/span> <span class=\"o\">&gt;<\/span> <span class=\"mi\">1<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span><span class=\"p\">)<\/span> <span class=\"o\">?<\/span> <span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"n\">f<\/span> <span class=\"o\">:<\/span> <span class=\"n\">g<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">sg_begin_pass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"p\">(<\/span><span class=\"n\">sg_pass<\/span><span class=\"p\">){<\/span> <span class=\"p\">.<\/span><span class=\"n\">action<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pass_action<\/span><span class=\"p\">,<\/span> <span class=\"p\">.<\/span><span class=\"n\">swapchain<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sglue_swapchain<\/span><span class=\"p\">()<\/span> <span class=\"p\">});<\/span>\n    <span class=\"n\">sg_end_pass<\/span><span class=\"p\">();<\/span>\n    <span class=\"n\">sg_commit<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">cleanup<\/span><span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">sg_shutdown<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"n\">sapp_desc<\/span> <span class=\"nf\">sokol_main<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span> <span class=\"n\">argc<\/span><span class=\"p\">,<\/span> <span class=\"kt\">char<\/span><span class=\"o\">*<\/span> <span class=\"n\">argv<\/span><span class=\"p\">[])<\/span> <span class=\"p\">{<\/span>\n    <span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span><span class=\"n\">argc<\/span><span class=\"p\">;<\/span> <span class=\"p\">(<\/span><span class=\"kt\">void<\/span><span class=\"p\">)<\/span><span class=\"n\">argv<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">sapp_desc<\/span><span class=\"p\">){<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">init_cb<\/span> <span class=\"o\">=<\/span> <span class=\"n\">init<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">frame_cb<\/span> <span class=\"o\">=<\/span> <span class=\"n\">frame<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">cleanup_cb<\/span> <span class=\"o\">=<\/span> <span class=\"n\">cleanup<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">window_title<\/span> <span class=\"o\">=<\/span> <span class=\"s\">\"clear\"<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">icon<\/span><span class=\"p\">.<\/span><span class=\"n\">sokol_default<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">true<\/span><span class=\"p\">,<\/span>\n        <span class=\"p\">.<\/span><span class=\"n\">logger<\/span><span class=\"p\">.<\/span><span class=\"n\">func<\/span> <span class=\"o\">=<\/span> <span class=\"n\">slog_func<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">};<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026we\u2019ll also need to make a few changes to our CMakeLists.txt file. Emscripten\nneeds to know that we want a program that runs in the browser. To do that we\u2019ll\nsimply change the executable file extension to <code class=\"language-plaintext highlighter-rouge\">.html<\/code>. Next we need to tell\nEmscripten to link with WebGL2.<\/p>\n\n<p>Open the CMakeLists.txt file and change the <code class=\"language-plaintext highlighter-rouge\">Emscripten<\/code> if-block like this:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">CMakeLists.txt<\/code><\/p>\n<div class=\"language-cmake highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">if<\/span> <span class=\"p\">(<\/span>CMAKE_SYSTEM_NAME STREQUAL Emscripten<span class=\"p\">)<\/span>\n    <span class=\"nb\">set<\/span><span class=\"p\">(<\/span>CMAKE_EXECUTABLE_SUFFIX .html<span class=\"p\">)<\/span>\n    <span class=\"nb\">target_link_options<\/span><span class=\"p\">(<\/span>hello PUBLIC -sUSE_WEBGL2=1<span class=\"p\">)<\/span>\n<span class=\"nb\">endif<\/span><span class=\"p\">()<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In VSCode press <code class=\"language-plaintext highlighter-rouge\">F7<\/code> to rebuild the program. This should generate three output files\nin the <code class=\"language-plaintext highlighter-rouge\">build\/Debug<\/code> directory:<\/p>\n\n<ul>\n  <li>hello.html<\/li>\n  <li>hello.js<\/li>\n  <li>hello.wasm<\/li>\n<\/ul>\n\n<p>Let\u2019s try to run that in the browser. On the command line in the project\ndirectory:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>emsdk\/upstream\/emscripten\/emrun build\/Debug\/hello.html\n<\/code><\/pre><\/div><\/div>\n\n<p>This should open the system\u2019s default web browser and you should see something like this,\nwith the orange rectangle cycling between yellow and red:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-7.png\" alt=\"Browser Screenshot\" \/><\/p>\n\n<p>\u2026let\u2019s get rid of the \u2018window chrome\u2019 by injecting our own minimal <code class=\"language-plaintext highlighter-rouge\">shell.html<\/code> file.<\/p>\n\n<p>In the project directory, create a file <code class=\"language-plaintext highlighter-rouge\">shell.html<\/code> looking like this:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">shell.html<\/code><\/p>\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\"&gt;<\/span>\n<span class=\"nt\">&lt;html&gt;<\/span>\n<span class=\"nt\">&lt;head&gt;<\/span>\n<span class=\"nt\">&lt;meta<\/span> <span class=\"na\">charset=<\/span><span class=\"s\">\"UTF-8\"<\/span><span class=\"nt\">\/&gt;<\/span>\n<span class=\"nt\">&lt;title&gt;<\/span>Clear<span class=\"nt\">&lt;\/title&gt;<\/span>\n<span class=\"nt\">&lt;style <\/span><span class=\"na\">type=<\/span><span class=\"s\">\"text\/css\"<\/span><span class=\"nt\">&gt;<\/span>\n<span class=\"nc\">.game<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nl\">position<\/span><span class=\"p\">:<\/span> <span class=\"nb\">absolute<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">top<\/span><span class=\"p\">:<\/span> <span class=\"m\">0px<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">left<\/span><span class=\"p\">:<\/span> <span class=\"m\">0px<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">margin<\/span><span class=\"p\">:<\/span> <span class=\"m\">0px<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">border<\/span><span class=\"p\">:<\/span> <span class=\"m\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">width<\/span><span class=\"p\">:<\/span> <span class=\"m\">100%<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">height<\/span><span class=\"p\">:<\/span> <span class=\"m\">100%<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">overflow<\/span><span class=\"p\">:<\/span> <span class=\"nb\">hidden<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">display<\/span><span class=\"p\">:<\/span> <span class=\"nb\">block<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">optimizeSpeed<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">-moz-crisp-edges<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">-o-crisp-edges<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">-webkit-optimize-contrast<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">optimize-contrast<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">crisp-edges<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">pixelated<\/span><span class=\"p\">;<\/span>\n    <span class=\"nl\">-ms-interpolation-mode<\/span><span class=\"p\">:<\/span> <span class=\"n\">nearest-neighbor<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"nt\">&lt;\/style&gt;<\/span>\n<span class=\"nt\">&lt;\/head&gt;<\/span>\n<span class=\"nt\">&lt;body<\/span> <span class=\"na\">style=<\/span><span class=\"s\">\"background:black\"<\/span><span class=\"nt\">&gt;<\/span>\n  <span class=\"nt\">&lt;canvas<\/span> <span class=\"na\">class=<\/span><span class=\"s\">\"game\"<\/span> <span class=\"na\">id=<\/span><span class=\"s\">\"canvas\"<\/span> <span class=\"na\">oncontextmenu=<\/span><span class=\"s\">\"event.preventDefault()\"<\/span><span class=\"nt\">&gt;&lt;\/canvas&gt;<\/span>\n  <span class=\"nt\">&lt;script <\/span><span class=\"na\">type=<\/span><span class=\"s\">\"text\/javascript\"<\/span><span class=\"nt\">&gt;<\/span>\n    <span class=\"kd\">var<\/span> <span class=\"nx\">Module<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n        <span class=\"na\">preRun<\/span><span class=\"p\">:<\/span> <span class=\"p\">[],<\/span>\n        <span class=\"na\">postRun<\/span><span class=\"p\">:<\/span> <span class=\"p\">[],<\/span>\n        <span class=\"na\">print<\/span><span class=\"p\">:<\/span> <span class=\"p\">(<\/span><span class=\"kd\">function<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n            <span class=\"k\">return<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">text<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n                <span class=\"nx\">text<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">Array<\/span><span class=\"p\">.<\/span><span class=\"nx\">prototype<\/span><span class=\"p\">.<\/span><span class=\"nx\">slice<\/span><span class=\"p\">.<\/span><span class=\"nx\">call<\/span><span class=\"p\">(<\/span><span class=\"nx\">arguments<\/span><span class=\"p\">).<\/span><span class=\"nx\">join<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\"> <\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n                <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">text<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">};<\/span>\n        <span class=\"p\">})(),<\/span>\n        <span class=\"na\">printErr<\/span><span class=\"p\">:<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">text<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n            <span class=\"nx\">text<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">Array<\/span><span class=\"p\">.<\/span><span class=\"nx\">prototype<\/span><span class=\"p\">.<\/span><span class=\"nx\">slice<\/span><span class=\"p\">.<\/span><span class=\"nx\">call<\/span><span class=\"p\">(<\/span><span class=\"nx\">arguments<\/span><span class=\"p\">).<\/span><span class=\"nx\">join<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\"> <\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n            <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">error<\/span><span class=\"p\">(<\/span><span class=\"nx\">text<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">},<\/span>\n        <span class=\"na\">canvas<\/span><span class=\"p\">:<\/span> <span class=\"p\">(<\/span><span class=\"kd\">function<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n            <span class=\"kd\">var<\/span> <span class=\"nx\">canvas<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">getElementById<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">canvas<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n            <span class=\"nx\">canvas<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">webglcontextlost<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">e<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">alert<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">FIXME: WebGL context lost, please reload the page<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span> <span class=\"nx\">e<\/span><span class=\"p\">.<\/span><span class=\"nx\">preventDefault<\/span><span class=\"p\">();<\/span> <span class=\"p\">},<\/span> <span class=\"kc\">false<\/span><span class=\"p\">);<\/span>\n            <span class=\"k\">return<\/span> <span class=\"nx\">canvas<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">})(),<\/span>\n        <span class=\"na\">setStatus<\/span><span class=\"p\">:<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">text<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span> <span class=\"p\">},<\/span>\n        <span class=\"na\">monitorRunDependencies<\/span><span class=\"p\">:<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">left<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span> <span class=\"p\">},<\/span>\n    <span class=\"p\">};<\/span>\n    <span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nx\">onerror<\/span> <span class=\"o\">=<\/span> <span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">event<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">onerror: <\/span><span class=\"dl\">\"<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">event<\/span><span class=\"p\">.<\/span><span class=\"nx\">message<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">};<\/span>\n  <span class=\"nt\">&lt;\/script&gt;<\/span>\n  {{{ SCRIPT }}}\n<span class=\"nt\">&lt;\/body&gt;<\/span>\n<span class=\"nt\">&lt;\/html&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026and in the <code class=\"language-plaintext highlighter-rouge\">CMakeLists.txt<\/code> file, change the linker options like this:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">CMakeLists.txt<\/code><\/p>\n<div class=\"language-cmake highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"nb\">target_link_options<\/span><span class=\"p\">(<\/span>hello PUBLIC -sUSE_WEBGL2=1 --shell-file=..\/shell.html<span class=\"p\">)<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026build the project again by pressing <code class=\"language-plaintext highlighter-rouge\">F7<\/code> and try opening the result\nin the browser:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>emsdk\/upstream\/emscripten\/emrun build\/Debug\/hello.html\n<\/code><\/pre><\/div><\/div>\n\n<p>\u2026the WebGL canvas should now stretch over the entire window client area:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-8.png\" alt=\"Browser Screenshot\" \/><\/p>\n\n<h3 id=\"browser-remote-debugging\">Browser Remote Debugging<\/h3>\n\n<p>Now on to the last step: making remote debugging work!<\/p>\n\n<p>First, <code class=\"language-plaintext highlighter-rouge\">.vscode\/launch.json<\/code> needs to be changed to start a Chrome remote debug session and a local web server:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">.vscode\/launch.json<\/code><\/p>\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"0.2.0\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"configurations\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Launch\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"chrome\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"request\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"launch\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"url\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"http:\/\/localhost:3000\/build\/Debug\/${command:cmake.launchTargetFilename}\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"preLaunchTask\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"StartServer\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>\u2026note the <code class=\"language-plaintext highlighter-rouge\">preLaunchTask<\/code>, this will start a web server using the Live Preview VSCode extension.<\/p>\n\n<p>To define the <code class=\"language-plaintext highlighter-rouge\">StartServer<\/code> task, create a file <code class=\"language-plaintext highlighter-rouge\">.vscode\/tasks.json<\/code> and populate it like this:<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">.vscode\/tasks.json<\/code><\/p>\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"2.0.0\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"tasks\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"label\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"StartServer\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"process\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"command\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"${input:startServer}\"<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"inputs\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"p\">{<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"id\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"startServer\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"command\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n            <\/span><span class=\"nl\">\"command\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"livePreview.runServerLoggingTask\"<\/span><span class=\"w\">\n        <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>\u2026and that\u2019s it!<\/p>\n\n<p>When pressing <code class=\"language-plaintext highlighter-rouge\">F5<\/code>, Chrome should now open and load our program:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-9.png\" alt=\"Browser Screenshot\" \/><\/p>\n\n<p>\u2026while the program is running in the browser, set a breakpoint in <code class=\"language-plaintext highlighter-rouge\">hello.c<\/code>\nat the start of function <code class=\"language-plaintext highlighter-rouge\">void frame(void)<\/code>. The debugger should now stop\nat the function and you can step through the code:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-10.png\" alt=\"VSCode Screenshot\" \/><\/p>\n\n<p>And that\u2019s it! You can now make changes to your code and then compile and\nrun\/debug with <code class=\"language-plaintext highlighter-rouge\">F5<\/code>. The only downside is the known issue that early breakpoints\nare not caught. There are two workarounds, first the one already mentioned to set\na breakpoint on the JS side in <code class=\"language-plaintext highlighter-rouge\">build\/Debug\/hello.js<\/code> in the <code class=\"language-plaintext highlighter-rouge\">callMain<\/code> function\nand stop on that first. This seems to catch any early breakpoints on the C side too.<\/p>\n\n<p>The second option for programs with a render loop is to simply restart the debug\nsession by pressing the \u2018Refresh\u2019 button in the VSCode debugger controls:<\/p>\n\n<p><img src=\"\/images\/emscripten-ide-11.png\" alt=\"VSCode Screenshot\" \/><\/p>\n\n<p>This will also catch early breakpoints on the C side, but will popup a warning\nthat the \u2018Live Preview\u2026` task is already running. This can simply be ignored.<\/p>\n\n<p>You can also find the project described in this blog post on Github:<\/p>\n\n<p><a href=\"https:\/\/github.com\/floooh\/vscode-emscripten-debugging\">https:\/\/github.com\/floooh\/vscode-emscripten-debugging<\/a><\/p>\n\n<h2 id=\"known-issues\">Known Issues<\/h2>\n\n<p>The list of issues I stumbled over, hopefully those will be fixed in the future:<\/p>\n\n<ul>\n  <li>The CMake Tools extension doesn\u2019t properly communicate the Emscripten system include\npath to the C\/C++ extension so that Intellisense doesn\u2019t work for system headers.<\/li>\n  <li>The WASM DWARF debugging extension doesn\u2019t catch early breakpoints on the C side\n(known issue).<\/li>\n  <li>The Live Preview extension pops up a warning when refreshing a debug session.<\/li>\n<\/ul>\n","pubDate":"Sat, 11 Nov 2023 00:00:00 +0000","link":"https:\/\/floooh.github.io\/2023\/11\/11\/emscripten-ide.html","guid":"https:\/\/floooh.github.io\/2023\/11\/11\/emscripten-ide.html"}]}}