[Fix] (MG_State/ProgramState): Handle version directive in shader source.

This commit is contained in:
BZLZHH
2025-11-01 10:07:36 +08:00
parent 9b0589840b
commit 8da681c720
4 changed files with 66 additions and 0 deletions
@@ -0,0 +1,45 @@
#include "ShaderSourceProcessor.h"
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
void PreprocessShaderSource(ShaderStage stage, String& source) {
ShaderProfile profile = ShaderProfile::Core;
SizeT versionPos = source.find("#version");
if (versionPos != String::npos) {
SizeT lineEnd = source.find('\n', versionPos);
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");
return;
}
SizeT firstLineEnd = source.find('\n');
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 != std::string::npos) {
source.replace(0, firstLineEnd + 1, replacement);
} else {
source = replacement;
}
}
}
} // namespace ShaderTranspiler
} // namespace MG_Util
} // namespace MobileGL