mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-20 16:31:57 +09:00
[Fix] (Magma): zero gl_Layer in draws into a non-layered framebuffer
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
#include "SpirvPasses/RebaseInstanceIndexPass.h"
|
||||
#include "SpirvPasses/ZeroBaseVertexPass.h"
|
||||
#include "SpirvPasses/LowerPixelCenterIntegerPass.h"
|
||||
#include "SpirvPasses/ZeroLayerOutputPass.h"
|
||||
#include "SpirvPasses/DeriveNumSubgroupsPass.h"
|
||||
#include "SpirvPasses/EmulateSubgroupsPass.h"
|
||||
#include "SpirvPasses/FixIterationRPBarrierPass.h"
|
||||
@@ -1997,6 +1998,17 @@ namespace MobileGL {
|
||||
true, enableSpirvValidation);
|
||||
}
|
||||
|
||||
bool ShaderCompiler::ZeroLayerOutputForVulkan(const Vector<Uint32>& inputBinary,
|
||||
Vector<uint32_t>& outputBinary,
|
||||
const bool enableSpirvValidation) {
|
||||
using namespace spvtools;
|
||||
Optimizer optimizer(SPV_ENV_VULKAN_1_1);
|
||||
optimizer.RegisterPass(ZeroLayerOutputPass::CreateZeroLayerOutputPass());
|
||||
|
||||
return RunOptimizerChecked("ZeroLayerOutputForVulkan", optimizer, inputBinary, outputBinary, true,
|
||||
enableSpirvValidation);
|
||||
}
|
||||
|
||||
bool ShaderCompiler::DeriveNumSubgroupsForVulkan(const Vector<Uint32>& inputBinary,
|
||||
Vector<uint32_t>& outputBinary,
|
||||
const bool enableSpirvValidation) {
|
||||
|
||||
@@ -353,6 +353,12 @@ namespace MobileGL {
|
||||
static bool LowerPixelCenterIntegerForVulkan(const Vector<Uint32>& inputBinary,
|
||||
Vector<uint32_t>& outputBinary,
|
||||
bool enableSpirvValidation = false);
|
||||
// Builds the variant of a pre-rasterization module that a non-layered framebuffer
|
||||
// draws with: every gl_Layer store writes 0, since GL ignores gl_Layer there. A
|
||||
// module that writes no Layer comes back unchanged. See ZeroLayerOutputPass.
|
||||
static bool ZeroLayerOutputForVulkan(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,109 @@
|
||||
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/ZeroLayerOutputPass.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 "ZeroLayerOutputPass.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 Layer, or nullptr if none.
|
||||
Instruction* FindLayerOutputVariable(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::Layer)
|
||||
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 ZeroLayerOutputPass::Process() {
|
||||
auto* irContext = context();
|
||||
auto* defUseMgr = irContext->get_def_use_mgr();
|
||||
auto* constantMgr = irContext->get_constant_mgr();
|
||||
auto* typeMgr = irContext->get_type_mgr();
|
||||
|
||||
Instruction* layer = FindLayerOutputVariable(irContext);
|
||||
if (layer == nullptr) return Status::SuccessWithoutChange;
|
||||
|
||||
// Gather the stores before mutating anything, so a shape the pass refuses leaves
|
||||
// the module exactly as it was.
|
||||
const uint32_t layerId = layer->result_id();
|
||||
std::vector<Instruction*> stores;
|
||||
Bool refused = false;
|
||||
defUseMgr->ForEachUser(layer, [&](Instruction* user) {
|
||||
if (refused || IsDeclarationUse(user)) return;
|
||||
if (user->opcode() == spv::Op::OpStore && user->GetSingleWordInOperand(0) == layerId) {
|
||||
stores.push_back(user);
|
||||
return;
|
||||
}
|
||||
refused = true;
|
||||
});
|
||||
if (refused) return Status::Failure;
|
||||
if (stores.empty()) return Status::SuccessWithoutChange;
|
||||
|
||||
const auto* pointerType = typeMgr->GetType(layer->type_id());
|
||||
const auto* pointer = pointerType ? pointerType->AsPointer() : nullptr;
|
||||
const auto* valueType = pointer ? pointer->pointee_type() : nullptr;
|
||||
if (valueType == nullptr || valueType->AsInteger() == nullptr) return Status::Failure;
|
||||
const uint32_t valueTypeId = typeMgr->GetId(valueType);
|
||||
const auto* zero = constantMgr->GetConstant(valueType, {0u});
|
||||
const Instruction* zeroInst = zero ? constantMgr->GetDefiningInstruction(zero, valueTypeId) : nullptr;
|
||||
if (valueTypeId == 0 || zeroInst == nullptr) return Status::Failure;
|
||||
const uint32_t zeroId = zeroInst->result_id();
|
||||
|
||||
for (Instruction* store : stores) store->SetInOperand(1, {zeroId});
|
||||
|
||||
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
|
||||
return Status::SuccessWithChange;
|
||||
}
|
||||
|
||||
spvtools::Optimizer::PassToken ZeroLayerOutputPass::CreateZeroLayerOutputPass() {
|
||||
return spvtools::Optimizer::PassToken(MakeUnique<ZeroLayerOutputPass>());
|
||||
}
|
||||
} // namespace ShaderTranspiler
|
||||
} // namespace MG_Util
|
||||
} // namespace MobileGL
|
||||
@@ -0,0 +1,43 @@
|
||||
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/ZeroLayerOutputPass.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 ignores gl_Layer when the framebuffer is not layered (GL 4.6 core 9.8): every
|
||||
// primitive lands in the one attached layer whatever the shader wrote. Vulkan has no
|
||||
// such rule - a Layer value at or past the framebuffer's layer count is undefined,
|
||||
// and Adreno offsets the primitive by it, out of the attachment
|
||||
// (KHR-GL4x.texture_cube_map_array.color_depth_attachments renders layer-face 1's
|
||||
// draw into face 2 of a single-layer attachment, and nothing into face 1). This pass
|
||||
// builds the module a non-layered framebuffer needs: every store to the Layer output
|
||||
// stores 0 instead.
|
||||
//
|
||||
// The renderer keeps this as a second module beside the original rather than
|
||||
// selecting with a specialization constant: a `layered ? value : 0` select on a
|
||||
// boolean specialization constant did not change the result on Adreno 750, while a
|
||||
// constant 0 store did.
|
||||
//
|
||||
// A use of the builtin other than a direct whole-variable store fails the pass, and
|
||||
// the caller then keeps the original module.
|
||||
class ZeroLayerOutputPass : public spvtools::opt::Pass {
|
||||
public:
|
||||
const char* name() const override { return "zero-layer-output"; }
|
||||
Status Process() override;
|
||||
|
||||
static spvtools::Optimizer::PassToken CreateZeroLayerOutputPass();
|
||||
};
|
||||
} // namespace ShaderTranspiler
|
||||
} // namespace MG_Util
|
||||
} // namespace MobileGL
|
||||
Reference in New Issue
Block a user