← 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 shader는 WGSL로 작성됩니다. WebKit의 WGSL compiler는 GPU process에서 실행되며, 먼저 source를 AST로 parsing한 뒤 type check를 수행합니다. 이 단계에서 type 추론과 constant folding이 함께 처리됩니다. 이어서 그 결과를 바탕으로 attribute 검증과 IO 검증을 거친 뒤, Metal Shading Language 코드를 생성하게 됩니다. 한편 web content process는 descriptor를 IPC로 전달하는 역할만 맡습니다. Render pipeline의 blend state는 target별 blend factor와 operation을 담은 descriptor로, GPU process가 이를 검증한 뒤 Metal enum으로 매핑합니다. Dual-source blending을 사용하면 fragment output location 하나가 두 개의 값을 가질 수 있고, 이 중 두 번째 값은 blend factor로 사용될 수 있습니다. 결과적으로 shader output layout이 pipeline blend state, 그리고 color target 개수와 서로 맞물리게 됩니다.

이 commit에서는 optional feature인 dual-source-blending과 WGSL dual_source_blending extension이 추가되었습니다. 함께 struct member attribute인 @blend_src(n)도 새로 도입되었습니다. 변경 범위는 parser와 AST부터 attribute/type/IO validation, Metal function writer까지 이어집니다. Host 쪽에서는 IDL과 serialization에 src1 계열 blend factor가 추가되었고, 두 번째 blend source의 사용 여부를 감지하는 pipeline 생성 로직도 구현되었습니다. 위의 member 단위 check는 extension 없이 쓰인 @blend_src를 거부합니다. 또한 값이 constant여야 한다는 조건을 요구하며, 0..1 범위를 벗어난 값도 거부합니다. 이 check와 별도로 validateBlendSrc(AST::Structure&)는 visit(AST::Structure&)에서 호출됩니다.

이제 fragment shader가 하나의 color location에 두 개의 output을 내보내고, 두 번째 값을 이용해 blending을 수행할 수 있습니다. 덕분에 channel별 alpha, 즉 LCD 스타일 alpha 같은 효과를 구현할 수 있게 되었습니다. GPU process의 WebGPU stack에 새로운 shader attribute, feature gate, IPC로 전달되는 enum 값이 추가되었고, 그 결과 shader output layout이 pipeline state와 맞물리게 되었습니다.

RenderPipeline.mm, IOValidator, ShaderModule.mm, Metal writer와 관련된 항목은 commit 설명을 근거로 정리한 내용입니다. 각 항목은 해당 파일에서 직접 확인할 필요가 있습니다.