mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-11 13:48:30 +09:00
Merge origin/dev (efd7b473) into readback overhaul - unify DirectGLES 2D-array target support under MapToBackendTextureTarget, keep canonical UNorm8 shadows for RGBA4/RGB565 (supersedes packed-word transfer types; GL_RGB565 aliases RGB5), keep upstream GLSL 330 normalization, anisotropy params, error-count semantics and VK clear/scissor fixes
This commit is contained in:
@@ -799,6 +799,9 @@ namespace MobileGL::MG_Util::BackendLoader {
|
||||
if (std::strcmp(extension, "GL_EXT_texture_norm16") == 0) {
|
||||
caps.SupportsNorm16Texture = true;
|
||||
}
|
||||
if (std::strcmp(extension, "GL_EXT_texture_filter_anisotropic") == 0) {
|
||||
caps.SupportsTextureFilterAnisotropy = true;
|
||||
}
|
||||
if (std::strcmp(extension, "GL_EXT_base_instance") == 0) {
|
||||
caps.SupportsBaseInstance = true;
|
||||
}
|
||||
|
||||
@@ -1031,6 +1031,9 @@ namespace MobileGL {
|
||||
String GLESShadingLanguageVersionString;
|
||||
Bool SupportsPersistentMapping = false;
|
||||
Bool SupportsNorm16Texture = false;
|
||||
// GL_EXT_texture_filter_anisotropic is present, so sampler/texture
|
||||
// anisotropy may be forwarded without raising GL_INVALID_ENUM in GLES.
|
||||
Bool SupportsTextureFilterAnisotropy = false;
|
||||
Bool SupportsBaseInstance = false;
|
||||
// GL_EXT_disjoint_timer_query is present in the extension string.
|
||||
Bool SupportsDisjointTimerQuery = false;
|
||||
|
||||
@@ -131,6 +131,9 @@ namespace MobileGL {
|
||||
case GL_RGB4:
|
||||
return TextureInternalFormat::RGB4;
|
||||
case GL_RGB5:
|
||||
// GL_RGB565 (GL 4.1 / ARB_ES2_compatibility, used directly by the GL CTS) is the
|
||||
// ES-facing rendition of the legacy RGB5 resolution.
|
||||
case GL_RGB565:
|
||||
return TextureInternalFormat::RGB5;
|
||||
case GL_RGB8:
|
||||
return TextureInternalFormat::RGB8;
|
||||
|
||||
@@ -113,7 +113,10 @@ namespace MobileGL {
|
||||
case TextureInternalFormat::RGB4:
|
||||
return GL_RGB4;
|
||||
case TextureInternalFormat::RGB5:
|
||||
return GL_RGB5;
|
||||
// Emit the ES-compatible GL_RGB565 rendition: desktop GL_RGB5 is not a legal
|
||||
// sized internalformat on OpenGL ES backends, GL_RGB565 is (and GL 4.1+
|
||||
// accepts it too via ARB_ES2_compatibility).
|
||||
return GL_RGB565;
|
||||
case TextureInternalFormat::RGB8:
|
||||
return GL_RGB8;
|
||||
case TextureInternalFormat::RGB8Snorm:
|
||||
|
||||
@@ -19,6 +19,220 @@ namespace {
|
||||
return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_';
|
||||
}
|
||||
|
||||
bool IsIdentifierStart(char ch) {
|
||||
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_';
|
||||
}
|
||||
|
||||
MobileGL::String MaskCommentsAndQuotedText(const MobileGL::String& source) {
|
||||
enum class Region { Code, SingleLineComment, MultiLineComment, QuotedText };
|
||||
|
||||
MobileGL::String masked = source;
|
||||
Region region = Region::Code;
|
||||
char quote = '\0';
|
||||
bool escaped = false;
|
||||
|
||||
for (SizeT pos = 0; pos < source.size(); pos++) {
|
||||
const char ch = source[pos];
|
||||
const char next = pos + 1 < source.size() ? source[pos + 1] : '\0';
|
||||
|
||||
if (region == Region::Code) {
|
||||
if (ch == '/' && next == '/') {
|
||||
masked[pos] = ' ';
|
||||
masked[pos + 1] = ' ';
|
||||
pos++;
|
||||
region = Region::SingleLineComment;
|
||||
} else if (ch == '/' && next == '*') {
|
||||
masked[pos] = ' ';
|
||||
masked[pos + 1] = ' ';
|
||||
pos++;
|
||||
region = Region::MultiLineComment;
|
||||
} else if (ch == '"' || ch == '\'') {
|
||||
masked[pos] = ' ';
|
||||
quote = ch;
|
||||
escaped = false;
|
||||
region = Region::QuotedText;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (region == Region::SingleLineComment) {
|
||||
if (ch == '\n' || ch == '\r') {
|
||||
region = Region::Code;
|
||||
} else {
|
||||
masked[pos] = ' ';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (region == Region::MultiLineComment) {
|
||||
if (ch == '*' && next == '/') {
|
||||
masked[pos] = ' ';
|
||||
masked[pos + 1] = ' ';
|
||||
pos++;
|
||||
region = Region::Code;
|
||||
} else if (ch != '\n' && ch != '\r') {
|
||||
masked[pos] = ' ';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch != '\n' && ch != '\r') {
|
||||
masked[pos] = ' ';
|
||||
}
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
} else if (ch == '\\') {
|
||||
escaped = true;
|
||||
} else if (ch == quote) {
|
||||
region = Region::Code;
|
||||
}
|
||||
}
|
||||
|
||||
return masked;
|
||||
}
|
||||
|
||||
void SkipDirectiveWhitespace(const MobileGL::String& source, SizeT& pos, SizeT lineEnd) {
|
||||
while (pos < lineEnd && std::isspace(static_cast<unsigned char>(source[pos]))) {
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
MobileGL::String ReadDirectiveIdentifier(const MobileGL::String& source, SizeT& pos, SizeT lineEnd) {
|
||||
if (pos >= lineEnd || !IsIdentifierStart(source[pos])) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const SizeT start = pos++;
|
||||
while (pos < lineEnd && IsIdentifierChar(source[pos])) {
|
||||
pos++;
|
||||
}
|
||||
return source.substr(start, pos - start);
|
||||
}
|
||||
|
||||
bool HasUtf8Bom(const MobileGL::String& source) {
|
||||
return source.size() >= 3 && static_cast<unsigned char>(source[0]) == 0xef &&
|
||||
static_cast<unsigned char>(source[1]) == 0xbb && static_cast<unsigned char>(source[2]) == 0xbf;
|
||||
}
|
||||
|
||||
struct ShaderLanguageInfo {
|
||||
unsigned version = 110;
|
||||
MobileGL::ShaderProfile profile = MobileGL::ShaderProfile::Core;
|
||||
SizeT versionDirectiveStart = MobileGL::String::npos;
|
||||
SizeT versionDirectiveEnd = MobileGL::String::npos;
|
||||
bool hasUtf8Bom = false;
|
||||
bool enablesGpuShader5 = false;
|
||||
|
||||
bool HasVersionDirective() const { return versionDirectiveStart != MobileGL::String::npos; }
|
||||
};
|
||||
|
||||
ShaderLanguageInfo InspectShaderLanguage(const MobileGL::String& source) {
|
||||
const MobileGL::String code = MaskCommentsAndQuotedText(source);
|
||||
ShaderLanguageInfo info;
|
||||
info.hasUtf8Bom = HasUtf8Bom(source);
|
||||
|
||||
SizeT lineStart = 0;
|
||||
while (lineStart < code.size()) {
|
||||
SizeT lineEnd = code.find('\n', lineStart);
|
||||
const bool hasLineBreak = lineEnd != MobileGL::String::npos;
|
||||
if (!hasLineBreak) {
|
||||
lineEnd = code.size();
|
||||
}
|
||||
|
||||
SizeT probe = lineStart;
|
||||
if (lineStart == 0 && info.hasUtf8Bom) {
|
||||
probe = 3;
|
||||
}
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
if (probe < lineEnd && code[probe] == '#') {
|
||||
const SizeT directiveStart = probe;
|
||||
probe++;
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
const MobileGL::String directive = ReadDirectiveIdentifier(code, probe, lineEnd);
|
||||
|
||||
if (directive == "version" && !info.HasVersionDirective()) {
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
unsigned version = 0;
|
||||
bool hasVersionDigits = false;
|
||||
while (probe < lineEnd && code[probe] >= '0' && code[probe] <= '9') {
|
||||
hasVersionDigits = true;
|
||||
version = version * 10 + static_cast<unsigned>(code[probe] - '0');
|
||||
probe++;
|
||||
}
|
||||
if (hasVersionDigits) {
|
||||
info.version = version;
|
||||
info.versionDirectiveStart = directiveStart;
|
||||
info.versionDirectiveEnd = lineEnd + (hasLineBreak ? 1 : 0);
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
const MobileGL::String profile = ReadDirectiveIdentifier(code, probe, lineEnd);
|
||||
if (profile == "es" || profile == "ES") {
|
||||
info.profile = MobileGL::ShaderProfile::ES;
|
||||
} else if (profile == "compatibility") {
|
||||
info.profile = MobileGL::ShaderProfile::Compatibility;
|
||||
} else {
|
||||
info.profile = MobileGL::ShaderProfile::Core;
|
||||
}
|
||||
}
|
||||
} else if (directive == "extension") {
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
const MobileGL::String extension = ReadDirectiveIdentifier(code, probe, lineEnd);
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
if (probe < lineEnd && code[probe] == ':') {
|
||||
probe++;
|
||||
SkipDirectiveWhitespace(code, probe, lineEnd);
|
||||
const MobileGL::String behavior = ReadDirectiveIdentifier(code, probe, lineEnd);
|
||||
const bool isGpuShader5 = extension == "GL_ARB_gpu_shader5" ||
|
||||
extension == "GL_NV_gpu_shader5";
|
||||
const bool enablesExtension = behavior == "enable" || behavior == "require" ||
|
||||
behavior == "warn";
|
||||
// Gate the whole source if it ever opts into either extension. This is deliberately
|
||||
// conservative around conditional directives and keeps legal sample qualifiers intact.
|
||||
info.enablesGpuShader5 = info.enablesGpuShader5 || (isGpuShader5 && enablesExtension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lineStart = lineEnd + (hasLineBreak ? 1 : 0);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
// Keep compatibility-profile handling on its pre-existing 460 path. Vulkan glslang does not accept that
|
||||
// profile today, and this legacy-sample fix must not broaden or otherwise alter that separate limitation.
|
||||
if (info.profile == MobileGL::ShaderProfile::Compatibility) {
|
||||
return "#version 460 compatibility\n";
|
||||
}
|
||||
|
||||
const bool useLegacyDesktopVersion =
|
||||
info.version < 400 && !info.enablesGpuShader5;
|
||||
return useLegacyDesktopVersion ? "#version 330 core\n" : "#version 460 core\n";
|
||||
}
|
||||
|
||||
void NormalizeVersionDirective(MobileGL::String& source, const ShaderLanguageInfo& info) {
|
||||
const MobileGL::String replacement = GetNormalizedVersionDirective(info);
|
||||
if (info.HasVersionDirective()) {
|
||||
source.replace(info.versionDirectiveStart, info.versionDirectiveEnd - info.versionDirectiveStart,
|
||||
replacement);
|
||||
if (info.hasUtf8Bom) {
|
||||
source.erase(0, 3);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.hasUtf8Bom) {
|
||||
source.erase(0, 3);
|
||||
}
|
||||
source.insert(0, replacement);
|
||||
}
|
||||
|
||||
bool HasSingleLineFunctionDefinition(const MobileGL::String& source, const MobileGL::String& functionName) {
|
||||
SizeT lineStart = 0;
|
||||
while (lineStart < source.size()) {
|
||||
@@ -153,12 +367,8 @@ namespace {
|
||||
}
|
||||
|
||||
SizeT FindAfterVersionDirective(const MobileGL::String& source) {
|
||||
const SizeT versionPos = source.find("#version");
|
||||
if (versionPos == MobileGL::String::npos) {
|
||||
return 0;
|
||||
}
|
||||
const SizeT lineEnd = source.find('\n', versionPos);
|
||||
return lineEnd == MobileGL::String::npos ? source.size() : lineEnd + 1;
|
||||
const ShaderLanguageInfo info = InspectShaderLanguage(source);
|
||||
return info.HasVersionDirective() ? info.versionDirectiveEnd : 0;
|
||||
}
|
||||
|
||||
bool IsExtensionAdvertised(MobileGL::GLExtension extension) {
|
||||
@@ -322,7 +532,7 @@ namespace {
|
||||
|
||||
void ModernizeLegacyGLSL(MobileGL::ShaderStage stage, MobileGL::String& source) {
|
||||
// Precision qualifiers (highp/mediump/lowp and default-precision statements) are legal and
|
||||
// ignored in the forced "#version 460 core" profile, so glslang handles them natively.
|
||||
// ignored in the normalized desktop core profiles, so glslang handles them natively.
|
||||
|
||||
ReplaceIdentifier(source, "texture2D", "texture");
|
||||
ReplaceIdentifier(source, "texture2DProj", "textureProj");
|
||||
@@ -367,6 +577,11 @@ namespace MobileGL {
|
||||
namespace MG_Util {
|
||||
namespace ShaderTranspiler {
|
||||
void PreprocessShaderSource(ShaderStage stage, String& source) {
|
||||
// Normalize while the inspector's source span still refers to the untouched input. Later passes
|
||||
// remove comments and directives, so any subsequent insertion re-inspects the current source.
|
||||
const ShaderLanguageInfo originalLanguage = InspectShaderLanguage(source);
|
||||
NormalizeVersionDirective(source, originalLanguage);
|
||||
|
||||
// remove multi-line comment
|
||||
size_t commentStartPos = source.find("/*");
|
||||
while (commentStartPos != String::npos) {
|
||||
@@ -404,43 +619,6 @@ namespace MobileGL {
|
||||
noperspectivePos = source.find(str_np);
|
||||
}
|
||||
|
||||
// force #version
|
||||
ShaderProfile profile = ShaderProfile::Core;
|
||||
SizeT versionPos = source.find("#version");
|
||||
SizeT lineEnd = source.find('\n', versionPos);
|
||||
|
||||
if (versionPos != String::npos) {
|
||||
String versionLine = source.substr(versionPos, lineEnd - versionPos);
|
||||
|
||||
if (versionLine.find("ES") != String::npos)
|
||||
profile = ShaderProfile::ES;
|
||||
else if (versionLine.find("compatibility") != String::npos)
|
||||
profile = ShaderProfile::Compatibility;
|
||||
else
|
||||
profile = ShaderProfile::Core;
|
||||
} else {
|
||||
profile = ShaderProfile::Core;
|
||||
source.insert(0, "#version 460 core\n");
|
||||
versionPos = 0;
|
||||
lineEnd = source.find('\n', versionPos);
|
||||
}
|
||||
|
||||
SizeT firstLineEnd = lineEnd;
|
||||
|
||||
if (profile != ShaderProfile::ES) {
|
||||
constexpr const char* versionDirectiveCore = "#version 460 core\n";
|
||||
constexpr const char* versionDirectiveCompat = "#version 460 compatibility\n";
|
||||
|
||||
const char* replacement =
|
||||
(profile == ShaderProfile::Compatibility) ? versionDirectiveCompat : versionDirectiveCore;
|
||||
|
||||
if (firstLineEnd != String::npos) {
|
||||
source.replace(versionPos, firstLineEnd - versionPos + 1, replacement);
|
||||
} else {
|
||||
source = replacement;
|
||||
}
|
||||
}
|
||||
|
||||
FilterUnsupportedGpuShaderInt64(source);
|
||||
CoerceUniformBlockPackingToStd140(source);
|
||||
|
||||
|
||||
@@ -310,10 +310,12 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
|
||||
// Color sized other
|
||||
case GL_RGB9_E5:
|
||||
case GL_R11F_G11F_B10F:
|
||||
case GL_RGB565:
|
||||
*outFormat = GL_RGB;
|
||||
break;
|
||||
case GL_RGB10_A2:
|
||||
case GL_RGB5_A1:
|
||||
case GL_RGBA4:
|
||||
*outFormat = GL_RGBA;
|
||||
break;
|
||||
case GL_RGB10_A2UI:
|
||||
@@ -324,13 +326,11 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
|
||||
case GL_R3_G3_B2:
|
||||
case GL_RGB4:
|
||||
case GL_RGB5:
|
||||
case GL_RGB565:
|
||||
case GL_RGB10:
|
||||
case GL_RGB12:
|
||||
*outFormat = GL_RGB;
|
||||
break;
|
||||
case GL_RGBA2:
|
||||
case GL_RGBA4:
|
||||
case GL_RGBA12:
|
||||
*outFormat = GL_RGBA;
|
||||
break;
|
||||
@@ -544,7 +544,6 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
|
||||
case GL_SRGB_ALPHA:
|
||||
*outType = GL_UNSIGNED_BYTE;
|
||||
break;
|
||||
|
||||
// Depth
|
||||
case GL_DEPTH_COMPONENT16:
|
||||
*outType = GL_UNSIGNED_SHORT;
|
||||
|
||||
Reference in New Issue
Block a user