Jump to content
  • Advertisement
Sign in to follow this  

HlslUnit released: a new unit testing library for HLSL shaders

This topic is 2108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


Thank you for the comments!


The most challenging part of implementing an HLSL interpreter was getting dynamic branching working. Because I wanted to emulate GPUs reasonably closely - in particular, running pixel shaders on a 2x2 quad so that partial derivative instructions (ddx, ddy) work correctly - I needed to support running multiple threads at the same time. But that means that when the interpreter executes a conditional branch instruction, some threads might take the branch, and some might not (as it happens, partial derivative instructions don't work inside dynamic branches, but still, I wanted to support multiple threads in general). So just like a real GPU (to the best of my limited knowledge) I execute both branches, and 'mask' the threads that aren't active for the given branch.


In order to make that work, I needed to rewrite the bytecode instructions into the form of basic blocks, where each block has a single entry point and a single exit point. Then, when taking a branch, the interpreter knows the index of the instruction where the branches will come back together. It uses that information to mask / unmask threads. It was quite 'fun' getting it figured out! In case anyone's interested, here are the papers / websites I learnt from:


SlimShader, the library that actually does most of the heavy lifting for HlslUnit, has a GUI tool that visualises how the instructions are broken down into basic blocks. You can load up the compiled binary for any shader you like (as long as it was compiled for SM4.0 or SM5.0), and see the corresponding control flow - which may (or may not! I only have a vague idea what happens at the hardware level) be similar to the shader code pre-processing that the GPU does.


By the way, the (C#) code for SlimShader, SlimShader.VirtualMachine and HlslUnit is here:



(Incidentally, in addition an HLSL interpreter, I also wrote a JITter, which compiles a new C# class on the fly, for a particular shader. But I don't use the JITter in HlslUnit, because the compilation time is too long when the shader is only getting executed once. I *do* use the JITter in my software rasterizer (https://github.com/tgjones/rasterizr), because it's a lot faster than the interpreter when rendering multiple frames.)




Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!