hlslcompute-shaderdirectx-12directcompute

Can I avoid exposing the kernel source code in Direct Compute?


Hiding kernel source or Offline compilation in Direct Compute

For example:

  1. In CUDA, kernel source will be built into .exe and others can't view the kernel source.
  2. In opencl, for first execution, kernel source in .cl file is compiled using clcreateprogramwithsource and program binaries can be stored and from next execution we can do offline compilation using clcreateprogramwithbinary.

My goal is to hide the kernel source code in .hlsl file from others and build the kernel source into final binary in Direct Compute.

I looked at sample in below link: https://github.com/walbourn/directx-sdk-samples/tree/main/BasicCompute11

This sample only explains online compilation technique.


Solution

  • d3d works in a reasonably similar way compared to OpenCL.

    D3DCompile/D3DCompileFromFile generates program binaries that can be stored (This bytecode works in a generic way across all platforms).

    Note that D3DCompile uses FXC, if you on dx12 the newer compiler (DXC) does pretty much the same thing.

    So you can store that bytecode and use device->CreateComputeShader, using that binary.

    If you look at the sample, you need to split the CreateComputeShader from the compile part to the upload part.