mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-21 00:41:57 +09:00
[Fix] (Shader): keep ESSL 3.10 block-array indexing and image-uniform rules for sources that declared an es version
This commit is contained in:
@@ -348,6 +348,48 @@ namespace MobileGL {
|
||||
return res;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// The first variable index into a uniform or shader storage block ARRAY, if any.
|
||||
class BlockArrayVariableIndexFinder final : public glslang::TIntermTraverser {
|
||||
public:
|
||||
const glslang::TIntermBinary* found = nullptr;
|
||||
|
||||
bool visitBinary(glslang::TVisit, glslang::TIntermBinary* node) override {
|
||||
if (found) return false;
|
||||
if (node->getOp() != glslang::EOpIndexIndirect) return true;
|
||||
const glslang::TType& base = node->getLeft()->getType();
|
||||
const glslang::TStorageQualifier storage = base.getQualifier().storage;
|
||||
if (base.getBasicType() == glslang::EbtBlock && base.isArray() &&
|
||||
(storage == glslang::EvqBuffer || storage == glslang::EvqUniform)) {
|
||||
found = node;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// ESSL 3.10 4.3.9: "All indices used to index a uniform or shader storage block array
|
||||
// must be constant integral expressions." ESSL 3.20 and GL_EXT/OES_gpu_shader5 relax
|
||||
// it to dynamically uniform; desktop GLSL has no such rule at all. The rewrite to
|
||||
// "#version 460 core" lost it, so it is put back here for the sources that declared
|
||||
// an `es` version - and only those. Returns the info-log line, or empty.
|
||||
String FindEsBlockArrayIndexViolation(const glslang::TShader& shader, const String& source) {
|
||||
const std::optional<EsOrigin> origin = FindEsOrigin(source);
|
||||
if (!origin || origin->version >= 320 || origin->gpuShader5) return {};
|
||||
glslang::TIntermediate* intermediate = shader.getIntermediate();
|
||||
if (intermediate == nullptr || intermediate->getTreeRoot() == nullptr) return {};
|
||||
BlockArrayVariableIndexFinder finder;
|
||||
intermediate->getTreeRoot()->traverse(&finder);
|
||||
if (!finder.found) return {};
|
||||
const glslang::TType& base = finder.found->getLeft()->getType();
|
||||
return std::format("ERROR: 0:{}: '[' : a {} block array must be indexed with a constant "
|
||||
"integral expression in ESSL {} without GL_EXT_gpu_shader5\n",
|
||||
finder.found->getLoc().line,
|
||||
base.getQualifier().storage == glslang::EvqBuffer ? "shader storage" : "uniform",
|
||||
origin->version);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Result<SharedPtr<glslang::TShader>> ShaderCompiler::CompileShader(const ShaderAttrib& attrib) {
|
||||
auto shaderType = attrib.shaderType;
|
||||
|
||||
@@ -361,7 +403,19 @@ namespace MobileGL {
|
||||
|
||||
const String source(attrib.sourceStr);
|
||||
auto result = ParseShaderSource(lang, shaderType, source, attrib.flags, attrib.env);
|
||||
if (result) return result;
|
||||
if (result) {
|
||||
// Part of the parse verdict, so an L1c hit and ClaimParsedShader's re-parse reach
|
||||
// the same answer as this parse did.
|
||||
if (String violation = FindEsBlockArrayIndexViolation(*result.value(), source);
|
||||
!violation.empty()) {
|
||||
ResultInfo r;
|
||||
r.log = "Error: [glslang] Cannot compile " + ConvertGLEnumToString(shaderType) + ":\n" +
|
||||
violation;
|
||||
r.errc = -2;
|
||||
return std::unexpected(r);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Legacy desktop sources are normalized to "#version 330 core" (with a marker on the
|
||||
// directive), which parses under stricter rules than the 460 they used to be forced
|
||||
|
||||
@@ -465,13 +465,18 @@ namespace {
|
||||
// Stamped onto the normalized directive when a legacy (or absent) desktop
|
||||
// version was rewritten to 330; consumed by RetargetLegacyVersionDirectiveTo460.
|
||||
constexpr const char* kNormalizedLegacyMarker = "/*mobilegl-normalized-legacy*/";
|
||||
// Stamped onto the directive an ES source is rewritten to, with the ESSL version it declared:
|
||||
// the rules ESSL has and desktop GLSL does not are enforced off it (FindEsOrigin), and it keeps
|
||||
// an ES source from sharing a parse verdict with a byte-identical desktop one.
|
||||
constexpr const char* kEsOriginMarkerPrefix = "/*mobilegl-es-origin:";
|
||||
|
||||
MobileGL::String GetNormalizedVersionDirective(const ShaderLanguageInfo& info) {
|
||||
if (info.profile == MobileGL::ShaderProfile::ES) {
|
||||
// Preserve the pre-existing behavior for standard lowercase "es" directives. MobileGL's Vulkan
|
||||
// glslang resource table cannot parse its ESSL built-ins today, even at ESSL 310, whereas the same
|
||||
// source is accepted through the normalized desktop core path.
|
||||
return "#version 460 core\n";
|
||||
return MobileGL::String("#version 460 core ") + kEsOriginMarkerPrefix + std::to_string(info.version) +
|
||||
"*/\n";
|
||||
}
|
||||
|
||||
// Keep compatibility-profile handling on its pre-existing 460 path. Vulkan glslang does not accept that
|
||||
@@ -1978,6 +1983,32 @@ namespace MobileGL {
|
||||
return names;
|
||||
}
|
||||
|
||||
std::optional<EsOrigin> FindEsOrigin(const String& preprocessedSource) {
|
||||
const SizeT markerStart = preprocessedSource.find(kEsOriginMarkerPrefix);
|
||||
if (markerStart == String::npos) return std::nullopt;
|
||||
SizeT probe = markerStart + std::char_traits<char>::length(kEsOriginMarkerPrefix);
|
||||
Uint esVersion = 0;
|
||||
bool hasDigits = false;
|
||||
while (probe < preprocessedSource.size() && preprocessedSource[probe] >= '0' &&
|
||||
preprocessedSource[probe] <= '9') {
|
||||
hasDigits = true;
|
||||
esVersion = esVersion * 10 + static_cast<Uint>(preprocessedSource[probe] - '0');
|
||||
if (esVersion > 1000) return std::nullopt; // absurd; not a marker this pipeline wrote
|
||||
probe++;
|
||||
}
|
||||
if (!hasDigits || preprocessedSource.compare(probe, 2, "*/") != 0) return std::nullopt;
|
||||
|
||||
// The #extension lines survive preprocessing, and enabledExtensions is already
|
||||
// closed under glslang's implication graph (GL_ANDROID_extension_pack_es31a turns
|
||||
// GL_EXT_gpu_shader5 on).
|
||||
const ShaderLanguageInfo info = InspectShaderLanguage(preprocessedSource);
|
||||
EsOrigin origin;
|
||||
origin.version = esVersion;
|
||||
origin.gpuShader5 = ExtensionTurnedOn(info, "GL_EXT_gpu_shader5") ||
|
||||
ExtensionTurnedOn(info, "GL_OES_gpu_shader5");
|
||||
return origin;
|
||||
}
|
||||
|
||||
String CollectEsPreambleMacroDefines(const String& preprocessedSource) {
|
||||
const SizeT markerStart = preprocessedSource.find(kEsPreambleMarkerPrefix);
|
||||
if (markerStart == String::npos) return {};
|
||||
|
||||
@@ -61,6 +61,16 @@ namespace MobileGL {
|
||||
// GetNormalizedVersionDirective records why that has not happened.
|
||||
String CollectEsPreambleMacroDefines(const String& preprocessedSource);
|
||||
|
||||
// What an ES-profile source declared before PreprocessShaderSource rewrote its
|
||||
// #version to desktop - recovered from the marker the rewrite leaves on the directive -
|
||||
// or nullopt for a source that did not declare an `es` version. The few rules ESSL has
|
||||
// and desktop GLSL does not are enforced off this, and only for such sources.
|
||||
struct EsOrigin {
|
||||
Uint version = 0; // 300, 310, 320
|
||||
Bool gpuShader5 = false; // GL_EXT_gpu_shader5 / GL_OES_gpu_shader5 turned on
|
||||
};
|
||||
std::optional<EsOrigin> FindEsOrigin(const String& preprocessedSource);
|
||||
|
||||
// The uniforms a shader declared as samplerExternalOES (GL_OES_EGL_image_external),
|
||||
// which PreprocessShaderSource turned into sampler2D and named in a marker: the
|
||||
// reflection reports them as GL_SAMPLER_EXTERNAL_OES. Empty for every other shader.
|
||||
|
||||
Reference in New Issue
Block a user