fix(legacy): A2C (Alpha to Coverage) and (antialias or MSAA) are mutually exclusive (#233)

This commit is contained in:
troublemaker52025
2026-08-07 16:27:27 +08:00
committed by GitHub
parent 90e551b4a1
commit a0a2e97c00
2 changed files with 49 additions and 1 deletions
+23 -1
View File
@@ -31,7 +31,7 @@ import { murmurhash2_32_gc, errorID, assertID, cclegacy, warnID } from '../../co
import {
BufferUsageBit, DynamicStateFlagBit, DynamicStateFlags, Feature, GetTypeSize, MemoryUsageBit, PrimitiveMode, Type, Color,
BlendState, BlendTarget, Buffer, BufferInfo, BufferViewInfo, DepthStencilState, DescriptorSet,
DescriptorSetInfo, DescriptorSetLayout, Device, RasterizerState, Sampler, Texture, Shader, PipelineLayout, deviceManager, UniformBlock,
DescriptorSetInfo, DescriptorSetLayout, Device, RasterizerState, Sampler, SampleCount, Texture, Shader, PipelineLayout, deviceManager, UniformBlock,
} from '../../gfx';
import { EffectAsset } from '../../asset/assets/effect-asset';
import { IProgramInfo, programLib } from './program-lib';
@@ -139,6 +139,28 @@ export class Pass {
if (bsInfo.isA2C !== undefined) { bs.isA2C = bsInfo.isA2C; }
if (bsInfo.isIndepend !== undefined) { bs.isIndepend = bsInfo.isIndepend; }
if (bsInfo.blendColor !== undefined) { bs.blendColor = bsInfo.blendColor as Color; }
// A2C (Alpha to Coverage) requires MSAA to be effective.
// Without multiple subsamples, A2C behaves like alpha-test at ~0.5 and should be disabled.
// Disable A2C when neither the pass nor the global framebuffer has MSAA enabled:
// - passWantsMultisample: pass explicitly requests isMultisample
// - globalMsaaEnabled: swapchain has MSAA (native: colorTexture.samples > X1;
// WebGL: antialias from gl.getContextAttributes(), since WebGL
// MSAA is controlled at context level, not texture level)
if (bs.isA2C) {
const gfxDevice = deviceManager.gfxDevice as any;
const gl: WebGL2RenderingContext | WebGLRenderingContext | null = gfxDevice.gl || null;
const ctxAttrs = gl && gl.getContextAttributes();
const antialias: boolean = !!(ctxAttrs && ctxAttrs.antialias);
const passWantsMultisample = (info.rasterizerState && info.rasterizerState.isMultisample === true);
const swapchain = deviceManager.swapchain;
const colorTex = swapchain && swapchain.colorTexture;
const msaaDisabled = (!colorTex || (colorTex && colorTex.samples <= SampleCount.X1))
|| !antialias || !passWantsMultisample;
if (msaaDisabled) {
bs.isA2C = false;
}
}
}
pass._rs.assign(info.rasterizerState as RasterizerState);
pass._dss.assign(info.depthStencilState as DepthStencilState);
+26
View File
@@ -119,6 +119,32 @@ void Pass::fillPipelineInfo(Pass *pass, const IPassInfoFull &info) {
if (info.blendState.has_value()) {
info.blendState.value().assignToGFXBlendState(pass->_blendState);
// A2C (Alpha to Coverage) requires MSAA to be effective.
// Without multiple subsamples, A2C behaves like alpha-test at ~0.5 and should be disabled.
// Disable A2C when neither the pass nor the global framebuffer has MSAA enabled:
// - passWantsMultisample: pass explicitly requests isMultisample
// - globalMsaaEnabled: swapchain colorTexture.samples > X1 (native backends only;
// WebGL MSAA is checked via gl.getContextAttributes() on the TS side)
if (pass->_blendState.isA2C) {
const bool passWantsMultisample =
(info.rasterizerState.has_value() && info.rasterizerState.value().isMultisample.has_value()
? info.rasterizerState.value().isMultisample.value() != 0
: pass->_rs.isMultisample != 0);
bool msaaDisabled = true;
const auto *device = gfx::Device::getInstance();
if (device) {
const auto &swapchains = device->getSwapchains();
if (!swapchains.empty() && swapchains[0] && swapchains[0]->getColorTexture()) {
msaaDisabled = (swapchains[0]->getColorTexture()->getInfo().samples <= gfx::SampleCount::X1) || !passWantsMultisample;
}
}
if (msaaDisabled) {
pass->_blendState.isA2C = 0;
}
}
}
if (info.rasterizerState.has_value()) {