An Experimental Approach to Printf in HLSL

Holiday hack sparks cheers, side-eyes, and FizzBuzz on your GPU

TLDR: A dev unveiled an experimental way to print messages from HLSL shaders by letting the CPU format text, not the GPU. Comments split between “finally!” and “just use proper tools,” with extra spice over Vulkan vs DirectX and performance worries—important because it could make shader debugging far easier.

The shader world grabbed popcorn as a dev dropped an experimental way to do “printf” in HLSL—the language behind DirectX graphics. It’s not official, it’s a holiday hack, but it prints those classic FizzBuzz lines right from your GPU by collecting strings at compile-time and letting the CPU do the formatting. Translation: the graphics card writes data; your computer turns it into text. Cue the crowd: one camp shouted “finally!” while another camp rolled their eyes, muttering “use real tools.” Some cheered that Vulkan and GLSL already have usable logging, while HLSL (used in DirectX) lags behind—classic platform rivalry vibes. Others warned this could tank performance if abused, and said proper debuggers like PIX or RenderDoc are the grown‑up choice. There’s also cross‑platform drama: getting this into Clang and DXC sounds doable, but making it play nice with SPIR‑V (Vulkan’s format) and Metal might be messy. Meanwhile, the memes wrote themselves: “FizzBuzz on a 4090,” “printf is my spirit animal,” and “training wheels for shader newbies.” The mood? Hopeful chaos. Whether you’re team HLSL or team Vulkan, everyone agreed on one thing: printing from shaders makes debugging way less painful—even if it’s just a festive experiment.

Key Points

  • An experimental, unofficial printf-style logging mechanism for HLSL is prototyped.
  • The approach avoids GPU-side string processing and removes strings from the shader binary.
  • A compile-time feature in Clang and DXC builds a deduplicated string table and maps literals to offsets via __builtin_hlsl_string_to_offset.
  • A sample compute shader demonstrates usage with dx::InitializeDebugStream and dx::printf; output ordering may vary.
  • The design requires no GPU driver changes but faces support challenges in DXC and when targeting SPIR-V or Metal.

Hottest takes

"Using printf in shaders is awesome" — exDM69
"Vulkan and GLSL have a usable printf out of the box" — exDM69
"All the string formatting is actually done on the CPU" — exDM69
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.