[Feat] (Subgroups): use native subgroups or emulate them on both backends

This commit is contained in:
2026-10-13 06:03:57 -04:00
parent cfbaffd757
commit d6b0eba8fe
50 changed files with 909 additions and 360 deletions
+43 -10
View File
@@ -162,9 +162,8 @@ TEST_F(ProgramTest, CompileFragment) {
CompileShader(fs);
}
TEST_F(ProgramTest, CompileVoxySubgroupProbeShader) {
char infoLog[1024] = "";
const char* csSrc = R"(#version 430
namespace {
const char* const kVoxySubgroupProbe = R"(#version 430
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_arithmetic : require
layout(local_size_x=32) in;
@@ -173,14 +172,48 @@ void main() {
}
)";
GLuint cs = CreateShader(GL_COMPUTE_SHADER);
ShaderSource(cs, 1, &csSrc, nullptr);
CompileShader(cs);
GLint CompileStatusOf(GLenum stage, const char* source, String& infoLog) {
GLuint shader = CreateShader(stage);
ShaderSource(shader, 1, &source, nullptr);
CompileShader(shader);
GLint compileStatus = GL_FALSE;
GetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
char log[1024] = "";
GetShaderInfoLog(shader, sizeof(log), nullptr, log);
infoLog = log;
DeleteShader(shader);
return compileStatus;
}
} // namespace
GLint compileStatus = GL_FALSE;
GetShaderiv(cs, GL_COMPILE_STATUS, &compileStatus);
GetShaderInfoLog(cs, sizeof(infoLog), nullptr, infoLog);
EXPECT_EQ(compileStatus, GL_TRUE) << infoLog;
// A `#extension GL_KHR_shader_subgroup_* : require` compiles exactly where the active backend
// offers that feature in that stage: against native compute subgroups with arithmetic it does;
// against a backend advertising no subgroups at all (no native support, emulation forced off) the
// frontend refuses it, rather than accepting a shader the backend would then fail on.
TEST_F(ProgramTest, CompileVoxySubgroupProbeShader) {
auto previousBackend = Move(MG_Backend::pActiveBackendObject);
const auto savedEmulation = MG_Config::Features.SubgroupEmulation;
auto backend = MakeUnique<MG_Backend::DirectVulkan::BackendObject_DirectVulkan>();
auto* backendPtr = backend.get();
MG_Backend::pActiveBackendObject = Move(backend);
String infoLog;
MG_External::VulkanCapabilities nativeCaps;
nativeCaps.SupportsShaderSubgroup = true;
nativeCaps.SubgroupSize = 64;
nativeCaps.SubgroupSupportedStages = VK_SHADER_STAGE_COMPUTE_BIT;
nativeCaps.SubgroupSupportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT | VK_SUBGROUP_FEATURE_ARITHMETIC_BIT;
backendPtr->ApplyVulkanCapabilitiesForTesting(nativeCaps);
EXPECT_EQ(CompileStatusOf(GL_COMPUTE_SHADER, kVoxySubgroupProbe, infoLog), GL_TRUE) << infoLog;
MG_Config::Features.SubgroupEmulation = MG_Config::QuirkOverride::ForceOff;
MG_External::VulkanCapabilities noCaps;
backendPtr->ApplyVulkanCapabilitiesForTesting(noCaps);
EXPECT_EQ(CompileStatusOf(GL_COMPUTE_SHADER, kVoxySubgroupProbe, infoLog), GL_FALSE);
EXPECT_NE(infoLog.find("is not available to this shader stage"), String::npos) << infoLog;
MG_Config::Features.SubgroupEmulation = savedEmulation;
MG_Backend::pActiveBackendObject = Move(previousBackend);
}
TEST_F(ProgramTest, CompileVoxyGpuShaderInt64QuadDecode) {
+17 -2
View File
@@ -6,6 +6,7 @@
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include <MG_Backend/SubgroupImplementation.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdlib>
@@ -858,18 +859,22 @@ TEST(FragmentInterpolationCapabilities, PlumbsGLESAndBothVulkanPropertyPaths) {
EXPECT_EQ(vkBackend.GetDynamicParameters().FragmentInterpolationOffsetBits, 4);
}
TEST(DirectVulkanSanity, AdvertisesSubgroupOnlyWhenVulkanReportsUsableSupport) {
// Native subgroups whenever Vulkan reports usable support; otherwise the 32-lane compute
// emulation by default, and nothing at all when MOBILEGL_SUBGROUP_EMULATION is ForceOff.
TEST(DirectVulkanSanity, AdvertisesNativeSubgroupsOrTheEmulation) {
using namespace MobileGL;
MG_Backend::DirectVulkan::BackendObject_DirectVulkan backend;
const auto savedEmulation = MG_Config::Features.SubgroupEmulation;
MG_External::VulkanCapabilities unsupportedCaps;
unsupportedCaps.SupportsShaderSubgroup = false;
unsupportedCaps.SubgroupSize = 32;
unsupportedCaps.SubgroupSupportedStages = VK_SHADER_STAGE_COMPUTE_BIT;
unsupportedCaps.SubgroupSupportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT;
backend.ApplyVulkanCapabilitiesForTesting(unsupportedCaps);
MG_Config::Features.SubgroupEmulation = MG_Config::QuirkOverride::ForceOff;
backend.ApplyVulkanCapabilitiesForTesting(unsupportedCaps);
const auto& unsupportedExtensions = backend.GetRendererInfo().RendererGLInfo.Extensions;
EXPECT_EQ(std::find(unsupportedExtensions.begin(), unsupportedExtensions.end(), E_GL_KHR_shader_subgroup),
unsupportedExtensions.end());
@@ -877,6 +882,16 @@ TEST(DirectVulkanSanity, AdvertisesSubgroupOnlyWhenVulkanReportsUsableSupport) {
EXPECT_EQ(backend.GetDynamicParameters().SubgroupSupportedStages, 0u);
EXPECT_EQ(backend.GetDynamicParameters().SubgroupSupportedFeatures, 0u);
MG_Config::Features.SubgroupEmulation = MG_Config::QuirkOverride::Auto;
backend.ApplyVulkanCapabilitiesForTesting(unsupportedCaps);
const auto& emulatedExtensions = backend.GetRendererInfo().RendererGLInfo.Extensions;
EXPECT_NE(std::find(emulatedExtensions.begin(), emulatedExtensions.end(), E_GL_KHR_shader_subgroup),
emulatedExtensions.end());
EXPECT_EQ(backend.GetDynamicParameters().SubgroupSize, MG_Backend::kEmulatedSubgroupSize);
EXPECT_EQ(backend.GetDynamicParameters().SubgroupSupportedStages, MG_Backend::kEmulatedSubgroupStages);
EXPECT_EQ(backend.GetDynamicParameters().SubgroupSupportedFeatures, MG_Backend::kEmulatedSubgroupFeatures);
MG_Config::Features.SubgroupEmulation = savedEmulation;
MG_External::VulkanCapabilities supportedCaps;
supportedCaps.SupportsShaderSubgroup = true;
supportedCaps.SubgroupSize = 32;
@@ -209,6 +209,24 @@ TEST(EmulateSubgroupsPass, LowersEveryCategoryToSharedMemory) {
EXPECT_GT(CountOpcode(output, spv::Op::OpControlBarrier), CountOpcode(input, spv::Op::OpControlBarrier));
}
// The stale-shared-load repair: every scratch read becomes an OpAtomicLoad of a uint word
// (floats, ints and vectors cross by bitcast), and the module still validates.
TEST(EmulateSubgroupsPass, ReadsScratchThroughAtomicLoadsWhenAsked) {
const Vector<Uint32> input = CompileStage(GL_COMPUTE_SHADER, kEveryCategorySource);
ASSERT_FALSE(input.empty());
Vector<Uint32> plain;
ASSERT_TRUE(ShaderCompiler::EmulateSubgroupsForVulkan(input, plain, 16384u, true, false));
EXPECT_EQ(CountOpcode(plain, spv::Op::OpAtomicLoad), 0u);
Vector<Uint32> atomic;
ASSERT_TRUE(ShaderCompiler::EmulateSubgroupsForVulkan(input, atomic, 16384u, true, true));
ASSERT_TRUE(Validates(atomic));
EXPECT_EQ(CountGroupNonUniform(atomic), 0u);
EXPECT_GT(CountOpcode(atomic, spv::Op::OpAtomicLoad), 0u);
EXPECT_GT(CountOpcode(atomic, spv::Op::OpBitcast), CountOpcode(plain, spv::Op::OpBitcast));
}
TEST(EmulateSubgroupsPass, IsIdempotent) {
const Vector<Uint32> input = CompileStage(GL_COMPUTE_SHADER, kEveryCategorySource);
ASSERT_FALSE(input.empty());
@@ -982,6 +982,26 @@ TEST_F(TranslationCacheTest, L2KeyMovesWithEveryGateThatSteersTheEsslChain) {
v.viewportIndexLoweringArmed = true;
variants.emplace_back("viewportIndexLoweringArmed", BuildEsslTranslationKey(v));
}
{ // arms EmulateSubgroupsPass on the ESSL path
EsslTranslationKeyInputs v = base;
v.subgroupEmulationArmed = true;
variants.emplace_back("subgroupEmulationArmed", BuildEsslTranslationKey(v));
}
{ // the emulation's scratch budget decides whether it declines
EsslTranslationKeyInputs v = base;
v.subgroupEmulationScratchBytes = 32768;
variants.emplace_back("subgroupEmulationScratchBytes", BuildEsslTranslationKey(v));
}
{ // arms FixIterationRPBarrierPass on the ESSL path
EsslTranslationKeyInputs v = base;
v.iterationRPBarrierFixArmed = true;
variants.emplace_back("iterationRPBarrierFixArmed", BuildEsslTranslationKey(v));
}
{ // the subgroup emulation reads its scratch through atomic loads
EsslTranslationKeyInputs v = base;
v.subgroupScratchAtomicReads = true;
variants.emplace_back("subgroupScratchAtomicReads", BuildEsslTranslationKey(v));
}
{ // arms DeferGeometryEmitsForEssl
EsslTranslationKeyInputs v = base;
v.deferGeometryEmitsArmed = true;