vtkSlang: typed shader customization for VTK’s WebGPU backend

Hi all,

I’d like to introduce vtkSlang, a library for typed shader customization in WebGPU renderers. Write a Slang type, select it by name, and let the compiler check the contract.

VTK’s OpenGL backend supports shader customization through string replacements at tags such as //VTK::Color::Impl. These are flexible, but a mistyped tag can silently do nothing, available variables depend on mapper state, and compilation errors point into generated GLSL. vtkSlang gives customization points explicit interfaces and reports invalid implementations in the supplied module.

I’m proposing its integration into VTK’s WebGPU backend and would love your feedback on the architecture.

Give your actor some stripes

prop->SetSlangModule(R"(
  module my_hooks;
  import vtkslang_hooks;
  public struct Stripes : IShaderHooks {
    float3 tint;
    public override void color(inout FragmentContext f) {
      if (frac(f.positionVC.y * 4.f) > 0.5f)
        f.diffuseColor = tint;
    }
  };)");
prop->SetSlangSpecialization("IShaderHooks", "Stripes");

const float rgb[3] = { 0.08f, 0.65f, 0.90f };
prop->GetFragmentCustomUniforms()->SetUniform3f("hooks.tint", rgb);

Same geometry, camera and lighting. Default shading on the left; the Stripes hook with a blue tint on the right.

Rename color to colour, and the compiler points straight to the invalid override:

error[E30854]: 'colour' marked as 'override' is not overriding any base declarations
 --> vtkslang-embedded:/my_hooks.slang:6:26
  |
6 | public override void colour(inout FragmentContext f) {
  |                      ^^^^^^ 'colour' marked as 'override' is not overriding any base declarations

That’s line 6, column 26 in the supplied module. One less mystery to hunt through generated shader code.

Hooks handle small adjustments; interfaces such as IMaterial and ILightingModel replace entire stages. Fields become named parameters, and [Range] and [Label] attributes supply metadata for UI controls.

Three pieces, clear responsibilities

  • vtkSlang is a BSD-3-Clause C++17 library, currently at 0.1.0. It owns the shared shader interfaces and shading sequence, compilation, reflection and parameter packing. It makes no WebGPU calls.
  • VTK::RenderingSlang wraps it through vtkSlangShaderManager, with VTK::CommonCore as its VTK dependency. It builds a stub when VTK_USE_SLANG is off.
  • VTK::RenderingWebGPU owns VTK’s shader programs, binding policy and GPU objects.

The separate library also serves anari-webgpu, allowing compatible materials and hooks to travel between renderers. Sharing the parameter packer has already fixed a matrix-layout bug in that integration.

The architecture document covers the interfaces, runtime flow, binding layout and API changes. Feedback on the design is welcome, and thanks to everyone who has helped shape it so far!

Sankhesh

Hi Sakhesh, this is a really cool project, I love to see a solution to replace string replacement in shaders. Could it also help long term to share more code between the OpenGL and WebGPU rendering back-ends?

This project has great utility, but have you thought of a different name than vtkSlang. I understand it was developed originally to support slang integration into VTK, but having vtk in the project name can lead to a misconception that your project depends upon VTK, which is untrue. I’ve not checked source in detail, but you don’t find_package(VTK) in cmake either.

Thanks, Louis. Yes, Slang can generate both GLSL and WGSL (GitHub - shader-slang/slang: Making it easier to work with shaders · GitHub), so the shared material and lighting implementations, along with the typed customization interfaces, could potentially support both backends.

OpenGL would still require its own integration and validation, including resource bindings, shader inputs and outputs, and compatibility with existing shader customizations. Given the effort involved and the likelihood that this backend will be retired in the near future, the current work is focused on WebGPU.

That’s a fair point, Jaswant. The name reflects the original VTK use case, but you’re right that the library itself does not depend on VTK.

A more general name could better communicate the library’s broader scope and avoid the confusion you described. It’s worth considering while the project is still at an early stage.

While WebGPU will become the default when VTK 10 is released, there is no near future with the OpenGL backend is “retired” in any shape or form. I do not expect a actual deprecation of the OpenGL backend for many years.

look forward to seeing the new name you come up with!