← All reports

Add support for dual-source-blending

Component: WebGPU WGSL | e53c6f0

Source/WebGPU/WGSL/AttributeValidator.cpp

+ if (auto* blendSrcAttribute = dynamicDowncast<AST::BlendSrcAttribute>(attribute)) {
+ if (!m_shaderModule.enabledExtensions().contains(Extension::DualSourceBlending)) [[unlikely]] {
+ error(attribute.span(), "@blend_src requires the 'dual_source_blending' extension to be enabled"_s);
+ return;
+ }
+
+ auto& constantValue = blendSrcAttribute->blendSrc().constantValue();
+ if (!constantValue) [[unlikely]] {
+ error(attribute.span(), "@blend_src constant value is missing"_s);
+ return;
+ }
+
+ auto blendSrcValue = constantValue->integerValue();
+ if (blendSrcValue < 0 || blendSrcValue > 1) [[unlikely]] {
+ error(attribute.span(), "@blend_src value must be either 0 or 1"_s);
+ return;

WebGPU shaders are written in WGSL. WebKit's WGSL compiler, running in the GPU process, parses source into an AST, type-checks it (inferring types and folding constants), runs attribute and IO validation on those results, and emits Metal Shading Language; the web content process only forwards descriptors over IPC. Render pipeline blend state is a descriptor of per-target blend factors and operations that the GPU process validates and maps to Metal enums. Dual-source blending lets one fragment output location carry two values, where the second can be used as a blend factor, which couples shader output layout to pipeline blend state and color-target count.

This commit adds the optional dual-source-blending feature and the WGSL dual_source_blending extension with a new @blend_src(n) struct-member attribute, spanning the parser, AST, attribute/type/IO validation and the Metal function writer. On the host side it adds the src1-family blend factors to the IDL and serialization, plus pipeline creation logic that detects use of a second blend source. The per-member check shown above rejects @blend_src without the extension, requires a constant, and rejects values outside 0..1; validateBlendSrc(AST::Structure&) is called from visit(AST::Structure&).

Fragment shaders can now emit two outputs at one color location and blend with the second, enabling effects like per-channel, LCD-style alpha. It adds a new shader attribute, feature gate and IPC-carried enum values to the WebGPU stack in the GPU process, coupling shader output layout to pipeline state.

The points touching RenderPipeline.mm, IOValidator, ShaderModule.mm and the Metal writer follow the commit description; confirm each against those files: