[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
@@ -1171,6 +1171,9 @@ namespace MobileGL::MG_Util::BackendLoader {
if (std::strcmp(extension, "GL_EXT_fragment_shading_rate") == 0) {
caps.SupportsFragmentShadingRate = true;
}
if (std::strcmp(extension, "GL_KHR_shader_subgroup") == 0) {
caps.SupportsShaderSubgroup = true;
}
if (std::strcmp(extension, "GL_EXT_fragment_shading_rate_primitive") == 0) {
caps.SupportsFragmentShadingRatePrimitive = true;
}
@@ -1292,6 +1295,27 @@ namespace MobileGL::MG_Util::BackendLoader {
if (caps.SupportsFragmentShadingRate) {
FillInFragmentShadingRateCapabilities(caps, glesFuncs);
}
if (caps.SupportsShaderSubgroup) {
// Through glesFuncs, never the bare entry point (that is MobileGL's own getter).
GLint size = 0, stages = 0, features = 0;
GLboolean quad = GL_FALSE;
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
glesFuncs.glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &size);
glesFuncs.glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &stages);
glesFuncs.glGetIntegerv(GL_SUBGROUP_SUPPORTED_FEATURES_KHR, &features);
glesFuncs.glGetBooleanv(GL_SUBGROUP_QUAD_ALL_STAGES_KHR, &quad);
const Bool answered = glesFuncs.glGetError() == GL_NO_ERROR;
caps.SubgroupSize = size > 0 ? static_cast<Uint32>(size) : 0u;
caps.SubgroupSupportedStages = static_cast<Uint32>(stages);
caps.SubgroupSupportedFeatures = static_cast<Uint32>(features);
caps.SubgroupQuadOperationsInAllStages = quad == GL_TRUE;
caps.SupportsShaderSubgroup = answered && caps.SubgroupSize > 0 &&
(caps.SubgroupSupportedStages & GL_COMPUTE_SHADER_BIT) != 0 &&
(caps.SubgroupSupportedFeatures & GL_SUBGROUP_FEATURE_BASIC_BIT_KHR) != 0;
}
MGLOG_I(" GL_KHR_shader_subgroup (native ES): %s size=%u stages=0x%x features=0x%x",
caps.SupportsShaderSubgroup ? "usable" : "absent", caps.SubgroupSize, caps.SubgroupSupportedStages,
caps.SubgroupSupportedFeatures);
// Core from ES 3.2 on, so an extension string is not required there; below 3.2 the
// extension is, and the pointer still has to have resolved either way.
const Bool esAtLeast32 = caps.GLESVersion.Major > 3 ||
@@ -1881,6 +1905,12 @@ namespace MobileGL::MG_Util::BackendLoader {
}
caps.MaxShaderStorageBufferBindings = maxShaderStorageBufferBindings;
caps.MaxTextureBufferSize = maxTextureBufferSize;
{
GLint sharedMemory = 0;
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
glesFuncs.glGetIntegerv(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, &sharedMemory);
if (glesFuncs.glGetError() == GL_NO_ERROR && sharedMemory > 0) caps.MaxComputeSharedMemorySize = sharedMemory;
}
// Through glesFuncs, like every other capability query here: a bare glGetIntegerv resolves
// to MobileGL's own exported entry point, which answers this pname from the very
// capability table being filled in - so the driver's real alignment never arrived and the
@@ -2078,6 +2108,23 @@ namespace MobileGL::MG_Util::BackendLoader {
? "true (DirectGLES defers a writing geometry stage's emits behind its writes)"
: "false");
switch (MG_Config::Features.EsprytAtomicSubgroupScratch) {
case MG_Config::QuirkOverride::ForceOn:
caps.SharedMemoryLoadsGoStale = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.SharedMemoryLoadsGoStale = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
caps.SharedMemoryLoadsGoStale = SelfTest::SharedMemoryLoadsGoStale(glesFuncs);
break;
}
MGLOG_I(" Shared-memory loads go stale across barrier(): %s",
caps.SharedMemoryLoadsGoStale
? "true (the subgroup emulation reads its scratch through atomic loads)"
: "false");
caps.IsAngleRenderer = caps.GLESRendererString.find("ANGLE") != String::npos;
caps.IsAngleLlvmpipeRenderer =
caps.IsAngleRenderer && caps.GLESRendererString.find("llvmpipe") != String::npos;
@@ -1324,6 +1324,11 @@ namespace MobileGL {
// then defers every emit of a geometry stage that writes memory behind those writes
// (DeferGeometryEmitsPass). Defaults FALSE: never assumed broken on an unmeasured driver.
Bool DropsGeometryWritesAfterEmit = false;
// True when two loads of one shared-memory word separated by a store and barrier()
// return the same (first) value (SelfTest::SharedMemoryLoadsGoStale - measured on
// Adreno). The subgroup emulation then reads its scratch through atomic loads.
// Defaults FALSE: never assumed broken on an unmeasured driver.
Bool SharedMemoryLoadsGoStale = false;
Int UniformBufferOffsetAlignment = 256;
// Its storage-buffer counterpart, queried separately because it is a separate limit:
// Adreno 830 answers 32 for GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT and 64 for
@@ -1380,6 +1385,18 @@ namespace MobileGL {
// initialisers are those minimums, for a context that rejects the query.
Int MaxComputeWorkGroupCount[3] = {65535, 65535, 65535};
Int MaxComputeWorkGroupSize[3] = {1024, 1024, 64};
// GL_MAX_COMPUTE_SHARED_MEMORY_SIZE; the initialiser is the ES 3.1 minimum. The budget
// the subgroup emulation's workgroup scratch has to fit in.
Int MaxComputeSharedMemorySize = 16384;
// GL_KHR_shader_subgroup on the ES driver, with what it answers for it. Extension-only
// in every ES version; the values are meaningful only when the flag is set, and the
// flag is only set when the driver also answers a usable size (> 0) with the compute
// stage and the basic feature - the same rule the Vulkan loader applies.
Bool SupportsShaderSubgroup = false;
Uint32 SubgroupSize = 0;
Uint32 SubgroupSupportedStages = 0; // GL_*_SHADER_BIT, as the ES query answers
Uint32 SubgroupSupportedFeatures = 0; // GL_SUBGROUP_FEATURE_*_BIT_KHR
Bool SubgroupQuadOperationsInAllStages = false;
Int MaxShaderStorageBufferBindings = 8;
Int MaxTextureBufferSize = 65536;
// GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT; 1 means the offset is unconstrained.
@@ -91,9 +91,6 @@ namespace MobileGL::MG_Util::BackendLoader {
return loaded;
}
Bool IsShaderSubgroupForcedDisabled() {
return MG_Config::Features.MagmaDisableSubgroup;
}
} // namespace
inline Version DecodeApiVersion(uint32_t version) {
@@ -266,8 +263,7 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.MaxShaderStorageBlockSize = static_cast<SizeT>(p.limits.maxStorageBufferRange);
const Bool supportsShaderSubgroup = vk.vkGetPhysicalDeviceProperties2 &&
HasUsableShaderSubgroupSupport(subgroupProps);
const Bool forceDisableShaderSubgroup = IsShaderSubgroupForcedDisabled();
caps.SupportsShaderSubgroup = supportsShaderSubgroup && !forceDisableShaderSubgroup;
caps.SupportsShaderSubgroup = supportsShaderSubgroup;
if (caps.SupportsShaderSubgroup) {
caps.SubgroupSize = subgroupProps.subgroupSize;
caps.SubgroupSupportedStages = subgroupProps.supportedStages;
@@ -280,12 +276,9 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.SubgroupQuadOperationsInAllStages = false;
}
MGLOG_I("Vulkan shader subgroup support: detected=%s advertised=%s size=%u stages=0x%x operations=0x%x",
supportsShaderSubgroup ? "true" : "false", caps.SupportsShaderSubgroup ? "true" : "false",
subgroupProps.subgroupSize, subgroupProps.supportedStages, subgroupProps.supportedOperations);
if (supportsShaderSubgroup && forceDisableShaderSubgroup) {
MGLOG_W("Vulkan shader subgroup support forced off by MOBILEGL_MAGMA_DISABLE_SUBGROUP");
}
MGLOG_I("Vulkan shader subgroup support: native=%s size=%u stages=0x%x operations=0x%x",
supportsShaderSubgroup ? "true" : "false", subgroupProps.subgroupSize,
subgroupProps.supportedStages, subgroupProps.supportedOperations);
return true;
}