Add support for dual-source-blending
Component: WebGPU WGSL | e53c6f0
Source/WebGPU/WGSL/AttributeValidator.cpp
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&)에서 호출됩니다.
Significance
이제 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와 맞물리게 되었습니다.
Audit directions
RenderPipeline.mm, IOValidator, ShaderModule.mm, Metal writer와 관련된 항목은 commit 설명을 근거로 정리한 내용입니다. 각 항목은 해당 파일에서 직접 확인할 필요가 있습니다.
- Index 검증의 edge case. Member check는
@blend_src뒤에 오는 값이 무엇이든 그대로integerValue()를 호출합니다. 따라서 정수가 아니거나 const가 아닌 인자를 넣어 볼 필요가 있습니다. float, bool, override로 선언된 값, abstract-int overflow 등이 대상입니다. 또한 range check 이전에 type 검증이 이루어지는지, validator가 실행되는 시점에 constant 값이 항상 채워져 있는지도 확인해야 합니다. validateBlendSrc의 member 간 규칙. 중복된 index, 짝 없이 단독으로 쓰인 0 또는 1, 한 쌍 사이의 type 불일치를 살펴볼 필요가 있습니다. 서로 다른@location값,@builtin과 함께 쓰인@blend_src, fragment output struct 바깥에서의 사용도 점검 대상입니다. 이때IOValidator::validateStructIO의 판단이 이와 일치하는지도 함께 확인해야 합니다.- Pipeline과 shader 간 정합성.
src1factor를 쓰려면@blend_src를 사용하는 shader와 정확히 하나의 color target이 필요합니다. 먼저 override, 한 module 안에 있는 여러 fragment entry point를 시도해 볼 수 있습니다. 또한parseFragmentReturnType이 검사한 것과 다른 entry point로 생성된 pipeline도 확인할 필요가 있습니다. 이런 경우 module 단위의usesDualSourceBlendingflag가 stale 상태가 될 가능성이 있습니다. - IPC 경로의 feature gating. 장악된 web process는 descriptor를 직접 전송할 수 있습니다. 그래서 device에 해당 feature가 없을 때, GPU process가 새로운
GPUBlendFactor/WebGPU::BlendFactor값을 거부하는지 확인해야 합니다. - Metal codegen. 두 번째 source에 대해 생성되는 attribute가 Metal의 attachment index 및 개수 제한을 지키는지 점검할 필요가 있습니다.
- Fuzzing.
enabledirective와Parser::parseAttribute주변을 겨냥한 WGSL fuzzing을 고려할 수 있습니다. 아울러 shader module과 조합한 blend state descriptor fuzzing도 유효한 방향입니다.