From: Mikko Rasa Date: Thu, 30 Sep 2021 21:11:46 +0000 (+0300) Subject: Make backend idenfication more generic X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e6077f9f25b794c174e1017c2c0763e77a6fddda;p=libs%2Fgl.git Make backend idenfication more generic The API and version queries are now in their own file and extension.h is reserved for the OpenGL extension mechanism. --- diff --git a/source/core/backend.cpp b/source/core/backend.cpp new file mode 100644 index 00000000..c3e159f4 --- /dev/null +++ b/source/core/backend.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include "backend.h" +#include "gl.h" + +using namespace std; + +namespace Msp { +namespace GL { + +Version::Version() +{ + major = 0; + minor = 0; +} + +Version::Version(unsigned short a, unsigned short i) +{ + major = a; + minor = i; +} + +Version::Version(const string &s) +{ + vector parts = split(s, '.'); + major = lexical_cast(parts[0]); + minor = lexical_cast(parts[1]); +} + +bool Version::operator>=(const Version &other) const +{ + return major>other.major || (major==other.major && minor>=other.minor); +} + + +GraphicsApi get_backend_api() +{ +#ifdef GL_ES_VERSION_2_0 + return OPENGL_ES; +#else + return OPENGL; +#endif +} + +inline Version get_gl_version() +{ + const char *gl_ver_ptr = reinterpret_cast(glGetString(GL_VERSION)); + if(!gl_ver_ptr) + throw runtime_error("OpenGL version not available"); + + string gl_ver = gl_ver_ptr; + if(!gl_ver.compare(0, 10, "OpenGL ES ")) + gl_ver.erase(0, 10); + + Version ver(gl_ver.substr(0, gl_ver.find(' '))); + + if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION")) + { + Version force_ver(force_ver_ptr); + if(force_ver + +namespace Msp { +namespace GL { + +enum GraphicsApi +{ + OPENGL, + OPENGL_ES +}; + +struct Version +{ + unsigned short major; + unsigned short minor; + + Version(); + Version(unsigned short, unsigned short); + Version(const std::string &); + + bool operator>=(const Version &) const; + bool operator<(const Version &o) const { return !(*this>=o); } + operator bool() const { return major || minor; } +}; + +/** Returns the backend for which the library was compiled. */ +GraphicsApi get_backend_api(); + +/** Returns the backend version number, as reported by the implementation. */ +const Version &get_backend_version(); + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/core/deviceinfo.cpp b/source/core/deviceinfo.cpp index 1526af36..37a04f8a 100644 --- a/source/core/deviceinfo.cpp +++ b/source/core/deviceinfo.cpp @@ -30,7 +30,7 @@ Limits::Limits() DeviceInfo::DeviceInfo() { - glsl_features.gl_api = get_gl_api(); + glsl_features.target_api = get_backend_api(); glsl_features.glsl_version = get_glsl_version(); glsl_features.arb_enhanced_layouts = ARB_enhanced_layouts; glsl_features.arb_explicit_attrib_location = ARB_explicit_attrib_location; diff --git a/source/core/extension.cpp b/source/core/extension.cpp index 8618cc3e..a55da9ba 100644 --- a/source/core/extension.cpp +++ b/source/core/extension.cpp @@ -28,31 +28,6 @@ using namespace std; namespace Msp { namespace GL { -Version::Version() -{ - major = 0; - minor = 0; -} - -Version::Version(unsigned short a, unsigned short i) -{ - major = a; - minor = i; -} - -Version::Version(const string &s) -{ - vector parts = split(s, '.'); - major = lexical_cast(parts[0]); - minor = lexical_cast(parts[1]); -} - -bool Version::operator>=(const Version &other) const -{ - return major>other.major || (major==other.major && minor>=other.minor); -} - - Extension::Extension(const char *n, InitFunc f): name(n), init_func(f), @@ -89,7 +64,7 @@ bool is_supported(const string &ext) if(!init_done) { - if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0)) + if(get_backend_api()==OPENGL && get_backend_version()>=Version(3, 0)) { typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint); FPtr_glGetStringi glGetStringi = reinterpret_cast(get_proc_address("glGetStringi")); @@ -115,7 +90,7 @@ bool is_supported(const string &ext) bool is_supported(const Version &core_version, const Version &deprecated_version) { - const Version &version = get_gl_version(); + const Version &version = get_backend_version(); if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE) return false; return (version>=core_version); @@ -144,7 +119,7 @@ bool is_disabled(const string &ext) /* AMD's uniform buffer objects only work with the core version of shaders. */ - if(get_gl_version()=Version(3, 0)) + if(get_backend_api()==OPENGL && get_backend_version()>=Version(3, 0)) { int mask; glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask); @@ -183,34 +149,6 @@ GLProfile get_gl_profile() return profile; } -inline Version _get_gl_version() -{ - const char *gl_ver_ptr = reinterpret_cast(glGetString(GL_VERSION)); - if(!gl_ver_ptr) - throw runtime_error("OpenGL version not available"); - - string gl_ver = gl_ver_ptr; - if(!gl_ver.compare(0, 10, "OpenGL ES ")) - gl_ver.erase(0, 10); - - Version ver(gl_ver.substr(0, gl_ver.find(' '))); - - if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION")) - { - Version force_ver(force_ver_ptr); - if(force_ver(glGetString(GL_SHADING_LANGUAGE_VERSION)); diff --git a/source/core/extension.h b/source/core/extension.h index cf824e24..77996357 100644 --- a/source/core/extension.h +++ b/source/core/extension.h @@ -2,16 +2,11 @@ #define MSP_GL_EXTENSION_H_ #include +#include "backend.h" namespace Msp { namespace GL { -enum GLApi -{ - OPENGL, - OPENGL_ES2 -}; - enum GLProfile { CORE_PROFILE, @@ -19,21 +14,6 @@ enum GLProfile }; -struct Version -{ - unsigned short major; - unsigned short minor; - - Version(); - Version(unsigned short, unsigned short); - Version(const std::string &); - - bool operator>=(const Version &) const; - bool operator<(const Version &o) const { return !(*this>=o); } - operator bool() const { return major || minor; } -}; - - /** Holds metadata about an extension. Evaluates to true if the extension is supported. @@ -83,15 +63,9 @@ the MSPGL_DISABLE_EXTENSIONS environment variable or implicitly as a workaround for a driver bug. Only intended for internal use. */ bool is_disabled(const std::string &); -/** Returns the API for which the library was compiled. */ -GLApi get_gl_api(); - /** Returns the OpenGL profile for the active context. */ GLProfile get_gl_profile(); -/** Returns the OpenGL version number, as reported by the implementation. */ -const Version &get_gl_version(); - /** Returns the GLSL version number, as reported by the implementation. */ const Version &get_glsl_version(); diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 33a7b21b..c27e8506 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -108,7 +108,7 @@ void Texture::apply_swizzle() if(swizzle==NO_SWIZZLE) return; - if(get_gl_api()==OPENGL_ES2) + if(get_backend_api()==OPENGL_ES) { set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]); set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]); diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 233e37ea..41734fe8 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -279,7 +279,7 @@ void Compiler::import(ModuleCache &mod_cache, const string &name) void Compiler::generate(Stage &stage) { - stage.required_features.gl_api = features.gl_api; + stage.required_features.target_api = features.target_api; if(module->shared.required_features.glsl_version>stage.required_features.glsl_version) stage.required_features.glsl_version = module->shared.required_features.glsl_version; diff --git a/source/glsl/features.cpp b/source/glsl/features.cpp index 78233bfc..a66eefc5 100644 --- a/source/glsl/features.cpp +++ b/source/glsl/features.cpp @@ -5,7 +5,7 @@ namespace GL { namespace SL { Features::Features(): - gl_api(OPENGL), + target_api(OPENGL), arb_enhanced_layouts(false), arb_explicit_attrib_location(false), arb_explicit_uniform_location(false), @@ -22,7 +22,7 @@ Features::Features(): Features Features::from_version(const Version &ver) { Features features; - features.gl_api = OPENGL; + features.target_api = OPENGL; features.glsl_version = ver; features.arb_enhanced_layouts = (ver>=Version(4, 40)); features.arb_explicit_attrib_location = (ver>=Version(1, 30)); diff --git a/source/glsl/features.h b/source/glsl/features.h index 8aee02ca..ca74d38a 100644 --- a/source/glsl/features.h +++ b/source/glsl/features.h @@ -1,7 +1,7 @@ #ifndef MSP_GL_SL_FEATURES_H_ #define MSP_GL_SL_FEATURES_H_ -#include +#include namespace Msp { namespace GL { @@ -9,7 +9,7 @@ namespace SL { struct Features { - GLApi gl_api; + GraphicsApi target_api; Version glsl_version; bool arb_enhanced_layouts; bool arb_explicit_attrib_location; diff --git a/source/glsl/finalize.cpp b/source/glsl/finalize.cpp index eee8a519..e444974a 100644 --- a/source/glsl/finalize.cpp +++ b/source/glsl/finalize.cpp @@ -261,7 +261,7 @@ void PrecisionConverter::visit(Block &block) void PrecisionConverter::visit(Precision &prec) { - if(stage->required_features.gl_api==OPENGL_ES2) + if(stage->required_features.target_api==OPENGL_ES) have_default.insert(prec.type); else nodes_to_remove.insert(&prec); @@ -269,7 +269,7 @@ void PrecisionConverter::visit(Precision &prec) void PrecisionConverter::visit(VariableDeclaration &var) { - if(stage->required_features.gl_api!=OPENGL_ES2) + if(stage->required_features.target_api!=OPENGL_ES) { var.precision.clear(); return; @@ -322,7 +322,7 @@ void LegacyConverter::apply(Stage &s, const Features &feat) NodeRemover().apply(s, nodes_to_remove); if(!stage->required_features.glsl_version) - stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10)); + stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10)); } else unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type))); @@ -372,7 +372,7 @@ bool LegacyConverter::supports_stage(Stage::Type st) const { if(st==Stage::GEOMETRY) { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 20)); else return check_version(Version(1, 50)); @@ -383,7 +383,7 @@ bool LegacyConverter::supports_stage(Stage::Type st) const bool LegacyConverter::supports_unified_interface_syntax() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 0)); else return check_version(Version(1, 30)); @@ -407,7 +407,7 @@ void LegacyConverter::visit(Assignment &assign) bool LegacyConverter::supports_unified_sampling_functions() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 0)); else return check_version(Version(1, 30)); @@ -452,7 +452,7 @@ void LegacyConverter::visit(FunctionCall &call) bool LegacyConverter::supports_interface_layouts() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 0)); else if(check_version(Version(3, 30))) return true; @@ -464,7 +464,7 @@ bool LegacyConverter::supports_interface_layouts() const bool LegacyConverter::supports_stage_interface_layouts() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 10)); else if(check_version(Version(4, 10))) return true; @@ -474,7 +474,7 @@ bool LegacyConverter::supports_stage_interface_layouts() const bool LegacyConverter::supports_centroid_sampling() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 0)); else if(check_version(Version(1, 20))) return true; @@ -484,7 +484,7 @@ bool LegacyConverter::supports_centroid_sampling() const bool LegacyConverter::supports_sample_sampling() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 20)); else if(check_version(Version(4, 0))) return true; @@ -494,7 +494,7 @@ bool LegacyConverter::supports_sample_sampling() const bool LegacyConverter::supports_uniform_location() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 10)); else if(check_version(Version(4, 30))) return true; @@ -504,7 +504,7 @@ bool LegacyConverter::supports_uniform_location() const bool LegacyConverter::supports_binding() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 10)); else return check_version(Version(4, 20)); @@ -591,7 +591,7 @@ void LegacyConverter::visit(VariableDeclaration &var) bool LegacyConverter::supports_interface_blocks(const string &iface) const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) { if(iface=="uniform") return check_version(Version(3, 0)); @@ -608,7 +608,7 @@ bool LegacyConverter::supports_interface_blocks(const string &iface) const bool LegacyConverter::supports_interface_block_location() const { - if(features.gl_api==OPENGL_ES2) + if(features.target_api==OPENGL_ES) return check_version(Version(3, 20)); else if(check_version(Version(4, 40))) return true; diff --git a/source/glsl/output.cpp b/source/glsl/output.cpp index 6ba06447..0a4af2d4 100644 --- a/source/glsl/output.cpp +++ b/source/glsl/output.cpp @@ -28,7 +28,7 @@ string Formatter::apply(Stage &s) if(ver) { append(format("#version %d%02d", ver.major, ver.minor)); - if(s.required_features.gl_api==OPENGL_ES2 && ver>=Version(3, 0)) + if(s.required_features.target_api==OPENGL_ES && ver>=Version(3, 0)) append(" es"); formatted += '\n'; }