[Fix] (Magma): ignore shader gl_SampleMask without multisample fragment ops

This commit is contained in:
2026-10-12 19:37:25 -04:00
parent 05d58a566a
commit 065d1582ba
13 changed files with 402 additions and 5 deletions
@@ -35,6 +35,7 @@
#include "SpirvPasses/ZeroBaseVertexPass.h"
#include "SpirvPasses/LowerPixelCenterIntegerPass.h"
#include "SpirvPasses/ZeroLayerOutputPass.h"
#include "SpirvPasses/FullCoverageSampleMaskPass.h"
#include "SpirvPasses/DeriveNumSubgroupsPass.h"
#include "SpirvPasses/EmulateSubgroupsPass.h"
#include "SpirvPasses/FixIterationRPBarrierPass.h"
@@ -2009,6 +2010,17 @@ namespace MobileGL {
enableSpirvValidation);
}
bool ShaderCompiler::FullCoverageSampleMaskForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
const bool enableSpirvValidation) {
using namespace spvtools;
Optimizer optimizer(SPV_ENV_VULKAN_1_1);
optimizer.RegisterPass(FullCoverageSampleMaskPass::CreateFullCoverageSampleMaskPass());
return RunOptimizerChecked("FullCoverageSampleMaskForVulkan", optimizer, inputBinary, outputBinary,
true, enableSpirvValidation);
}
bool ShaderCompiler::DeriveNumSubgroupsForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
const bool enableSpirvValidation) {
@@ -359,6 +359,13 @@ namespace MobileGL {
static bool ZeroLayerOutputForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Builds the variant of a fragment module that a draw without multisample fragment
// operations (single-sample framebuffer, or MULTISAMPLE disabled) uses: every
// gl_SampleMask store writes all-ones, since GL ignores the mask there. A module
// that writes no SampleMask comes back unchanged. See FullCoverageSampleMaskPass.
static bool FullCoverageSampleMaskForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Replaces compute gl_NumSubgroups loads with ceil(workgroup invocations /
// gl_SubgroupSize). DirectVulkan only; this repairs drivers whose builtin
// disagrees with the subgroup IDs the same dispatch emits (Adreno reports 1
@@ -0,0 +1,148 @@
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/FullCoverageSampleMaskPass.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "FullCoverageSampleMaskPass.h"
#include "spirv.hpp"
#include "source/opt/constants.h"
#include "source/opt/def_use_manager.h"
#include "source/opt/instruction.h"
#include "source/opt/ir_context.h"
#include "source/opt/module.h"
#include "source/opt/type_manager.h"
#include "source/util/make_unique.h"
#include <vector>
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
// Returns the Output OpVariable decorated BuiltIn SampleMask, or nullptr if none.
Instruction* FindSampleMaskOutputVariable(IRContext* context) {
auto* defUseMgr = context->get_def_use_mgr();
for (auto& annotation : context->annotations()) {
if (annotation.opcode() != spv::Op::OpDecorate || annotation.NumInOperands() < 3) continue;
if (static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1)) !=
spv::Decoration::BuiltIn)
continue;
if (static_cast<spv::BuiltIn>(annotation.GetSingleWordInOperand(2)) !=
spv::BuiltIn::SampleMask)
continue;
Instruction* variable = defUseMgr->GetDef(annotation.GetSingleWordInOperand(0));
if (variable != nullptr && variable->opcode() == spv::Op::OpVariable &&
static_cast<spv::StorageClass>(variable->GetSingleWordInOperand(0)) ==
spv::StorageClass::Output) {
return variable;
}
}
return nullptr;
}
Bool IsDeclarationUse(const Instruction* user) {
switch (user->opcode()) {
case spv::Op::OpDecorate:
case spv::Op::OpDecorateId:
case spv::Op::OpDecorateString:
case spv::Op::OpName:
case spv::Op::OpEntryPoint:
return true;
default:
return false;
}
}
} // namespace
spvtools::opt::Pass::Status FullCoverageSampleMaskPass::Process() {
auto* irContext = context();
auto* defUseMgr = irContext->get_def_use_mgr();
auto* constantMgr = irContext->get_constant_mgr();
auto* typeMgr = irContext->get_type_mgr();
Instruction* sampleMask = FindSampleMaskOutputVariable(irContext);
if (sampleMask == nullptr) return Status::SuccessWithoutChange;
// Gather the stores before mutating anything, so a shape the pass refuses leaves
// the module exactly as it was. Whole-array stores and element stores are kept
// apart: they need an all-ones array and an all-ones element respectively.
const uint32_t sampleMaskId = sampleMask->result_id();
std::vector<Instruction*> arrayStores;
std::vector<Instruction*> elementStores;
Bool refused = false;
defUseMgr->ForEachUser(sampleMask, [&](Instruction* user) {
if (refused || IsDeclarationUse(user)) return;
if (user->opcode() == spv::Op::OpStore && user->GetSingleWordInOperand(0) == sampleMaskId) {
arrayStores.push_back(user);
return;
}
if ((user->opcode() == spv::Op::OpAccessChain ||
user->opcode() == spv::Op::OpInBoundsAccessChain) &&
user->NumInOperands() == 2) {
const uint32_t chainId = user->result_id();
defUseMgr->ForEachUser(user, [&](Instruction* chainUser) {
if (refused || IsDeclarationUse(chainUser)) return;
if (chainUser->opcode() == spv::Op::OpStore &&
chainUser->GetSingleWordInOperand(0) == chainId) {
elementStores.push_back(chainUser);
return;
}
refused = true;
});
return;
}
refused = true;
});
if (refused) return Status::Failure;
if (arrayStores.empty() && elementStores.empty()) return Status::SuccessWithoutChange;
const auto* pointerType = typeMgr->GetType(sampleMask->type_id());
const auto* pointer = pointerType ? pointerType->AsPointer() : nullptr;
const auto* arrayType = pointer ? pointer->pointee_type() : nullptr;
const auto* array = arrayType ? arrayType->AsArray() : nullptr;
const auto* elementType = array ? array->element_type() : nullptr;
const auto* integer = elementType ? elementType->AsInteger() : nullptr;
if (integer == nullptr || integer->width() != 32) return Status::Failure;
const uint32_t elementTypeId = typeMgr->GetId(elementType);
const auto* allOnes = constantMgr->GetConstant(elementType, {0xffffffffu});
const Instruction* allOnesInst =
allOnes ? constantMgr->GetDefiningInstruction(allOnes, elementTypeId) : nullptr;
if (elementTypeId == 0 || allOnesInst == nullptr) return Status::Failure;
for (Instruction* store : elementStores) store->SetInOperand(1, {allOnesInst->result_id()});
if (!arrayStores.empty()) {
// The array's length is a constant (the builtin is sized at link time); a
// specialization-constant length is not something this pass can fill.
const auto& lengthInfo = array->length_info();
if (lengthInfo.words.size() != 2 ||
lengthInfo.words[0] != spvtools::opt::analysis::Array::LengthInfo::kConstant) {
return Status::Failure;
}
const uint32_t length = lengthInfo.words[1];
const uint32_t arrayTypeId = typeMgr->GetId(arrayType);
std::vector<uint32_t> elementIds(length, allOnesInst->result_id());
const auto* allOnesArray = constantMgr->GetConstant(arrayType, elementIds);
const Instruction* allOnesArrayInst =
allOnesArray ? constantMgr->GetDefiningInstruction(allOnesArray, arrayTypeId) : nullptr;
if (arrayTypeId == 0 || allOnesArrayInst == nullptr) return Status::Failure;
for (Instruction* store : arrayStores) store->SetInOperand(1, {allOnesArrayInst->result_id()});
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken FullCoverageSampleMaskPass::CreateFullCoverageSampleMaskPass() {
return spvtools::Optimizer::PassToken(MakeUnique<FullCoverageSampleMaskPass>());
}
} // namespace ShaderTranspiler
} // namespace MG_Util
} // namespace MobileGL
@@ -0,0 +1,42 @@
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/FullCoverageSampleMaskPass.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
#include "source/opt/pass.h"
#include "spirv-tools/optimizer.hpp"
#include <Includes.h>
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
// GL applies a fragment shader's gl_SampleMask only among the multisample fragment
// operations, which make no change "if MULTISAMPLE is disabled, or if the value of
// SAMPLE_BUFFERS is not one" (GL 4.6 core 17.3.3). Vulkan ANDs the SampleMask output
// with coverage at every rasterizationSamples, so at one sample a mask with bit 0
// clear discards the fragment (KHR-GL4x.sample_variables.mask.*.samples_0 drew nothing
// into a single-sample texture). This pass builds the fragment module such a draw
// uses: every store to the SampleMask output stores all-ones, which leaves coverage
// as the rasterizer produced it.
//
// Kept as a second module beside the original, like ZeroLayerOutputPass, so the
// multisample draws of the same program keep the shader's mask.
//
// Handles whole-array stores and stores through an access chain into the array. Any
// other use of the builtin (a read-back of the output, a chain used for anything but
// a store) fails the pass, and the caller then keeps the original module.
class FullCoverageSampleMaskPass : public spvtools::opt::Pass {
public:
const char* name() const override { return "full-coverage-sample-mask"; }
Status Process() override;
static spvtools::Optimizer::PassToken CreateFullCoverageSampleMaskPass();
};
} // namespace ShaderTranspiler
} // namespace MG_Util
} // namespace MobileGL