From d16d28d2ccf7c6255204f02975834f713ff1df08 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 9 Nov 2021 14:07:01 +0200 Subject: [PATCH] Move backend information into Device --- source/backends/opengl/backend_opengl.cpp | 33 --------- source/backends/opengl/device_backend.cpp | 74 +++++++++++++++++++ source/backends/opengl/device_backend.h | 2 + source/backends/opengl/deviceinfo_backend.cpp | 60 --------------- source/backends/opengl/extension.cpp | 11 ++- .../backends/opengl/pipelinestate_backend.cpp | 6 +- source/backends/opengl/program_backend.cpp | 4 +- source/builders/sequencebuilder.cpp | 4 +- source/core/backend.h | 3 - source/core/device.cpp | 3 + source/core/device.h | 31 ++++++++ source/core/deviceinfo.cpp | 13 ---- source/core/deviceinfo.h | 43 ----------- source/core/module.cpp | 7 +- source/core/sampler.cpp | 4 +- source/core/texture2dmultisample.cpp | 4 +- source/core/uniformblock.cpp | 4 +- source/core/vertexsetup.cpp | 4 +- 18 files changed, 136 insertions(+), 174 deletions(-) delete mode 100644 source/backends/opengl/deviceinfo_backend.cpp delete mode 100644 source/core/deviceinfo.cpp delete mode 100644 source/core/deviceinfo.h diff --git a/source/backends/opengl/backend_opengl.cpp b/source/backends/opengl/backend_opengl.cpp index ce8fc17b..46d1ce20 100644 --- a/source/backends/opengl/backend_opengl.cpp +++ b/source/backends/opengl/backend_opengl.cpp @@ -1,9 +1,4 @@ -#include -#include #include "backend.h" -#include "gl.h" - -using namespace std; namespace Msp { namespace GL { @@ -17,33 +12,5 @@ GraphicsApi get_backend_api() #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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "device.h" #include "device_backend.h" +#include "gl.h" + +using namespace std; namespace Msp { namespace GL { @@ -7,5 +25,61 @@ OpenGLDevice::OpenGLDevice(Graphics::Window &wnd, const Graphics::GLOptions &opt context(wnd, opts) { } +void OpenGLDevice::fill_info() +{ + DeviceInfo &info = static_cast(this)->info; + + if(const char *gl_ver_ptr = reinterpret_cast(glGetString(GL_VERSION))) + { + 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(&lim.max_clip_planes)); + if(ARB_vertex_shader) + { + glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, reinterpret_cast(&lim.max_vertex_attributes)); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, reinterpret_cast(&lim.max_texture_bindings)); + } + if(EXT_framebuffer_object) + glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, reinterpret_cast(&lim.max_color_attachments)); + if(EXT_framebuffer_multisample) + glGetIntegerv(GL_MAX_SAMPLES, reinterpret_cast(&lim.max_samples)); + if(ARB_uniform_buffer_object) + { + glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, reinterpret_cast(&lim.max_uniform_bindings)); + glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, reinterpret_cast(&lim.uniform_buffer_alignment)); + } + if(EXT_texture_filter_anisotropic) + glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &lim.max_anisotropy); + + SL::Features &feat = info.glsl_features; + feat.target_api = get_backend_api(); + feat.glsl_version = get_glsl_version(); + feat.arb_enhanced_layouts = ARB_enhanced_layouts; + feat.arb_explicit_attrib_location = ARB_explicit_attrib_location; + feat.arb_explicit_uniform_location = ARB_explicit_uniform_location; + feat.arb_gpu_shader5 = ARB_gpu_shader5; + feat.arb_separate_shader_objects = ARB_separate_shader_objects; + feat.arb_uniform_buffer_object = ARB_uniform_buffer_object; + feat.ext_gpu_shader4 = EXT_gpu_shader4; + feat.ext_texture_array = EXT_texture_array; + feat.uniform_binding_range = lim.max_uniform_bindings; + feat.texture_binding_range = lim.max_texture_bindings; +} + } // namespace GL } // namespace Msp diff --git a/source/backends/opengl/device_backend.h b/source/backends/opengl/device_backend.h index e64352d3..f3a8e480 100644 --- a/source/backends/opengl/device_backend.h +++ b/source/backends/opengl/device_backend.h @@ -14,6 +14,8 @@ protected: OpenGLDevice(Graphics::Window &, const Graphics::GLOptions &); + void fill_info(); + Graphics::GLContext &get_context() { return context; } }; diff --git a/source/backends/opengl/deviceinfo_backend.cpp b/source/backends/opengl/deviceinfo_backend.cpp deleted file mode 100644 index df085dc7..00000000 --- a/source/backends/opengl/deviceinfo_backend.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "deviceinfo.h" -#include "gl.h" - -namespace Msp { -namespace GL { - -Limits::Limits() -{ - glGetIntegerv(GL_MAX_CLIP_PLANES, reinterpret_cast(&max_clip_planes)); - if(ARB_vertex_shader) - { - glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, reinterpret_cast(&max_vertex_attributes)); - glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, reinterpret_cast(&max_texture_bindings)); - } - if(EXT_framebuffer_object) - glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, reinterpret_cast(&max_color_attachments)); - if(EXT_framebuffer_multisample) - glGetIntegerv(GL_MAX_SAMPLES, reinterpret_cast(&max_samples)); - if(ARB_uniform_buffer_object) - { - glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, reinterpret_cast(&max_uniform_bindings)); - glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, reinterpret_cast(&uniform_buffer_alignment)); - } - if(EXT_texture_filter_anisotropic) - glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &max_anisotropy); -} - - -DeviceInfo::DeviceInfo() -{ - 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; - glsl_features.arb_explicit_uniform_location = ARB_explicit_uniform_location; - glsl_features.arb_gpu_shader5 = ARB_gpu_shader5; - glsl_features.arb_separate_shader_objects = ARB_separate_shader_objects; - glsl_features.arb_uniform_buffer_object = ARB_uniform_buffer_object; - glsl_features.ext_gpu_shader4 = EXT_gpu_shader4; - glsl_features.ext_texture_array = EXT_texture_array; - glsl_features.uniform_binding_range = limits.max_uniform_bindings; - glsl_features.texture_binding_range = limits.max_texture_bindings; -} - -} // namespace Msp -} // namespace GL diff --git a/source/backends/opengl/extension.cpp b/source/backends/opengl/extension.cpp index a55da9ba..84f8bd0a 100644 --- a/source/backends/opengl/extension.cpp +++ b/source/backends/opengl/extension.cpp @@ -10,6 +10,7 @@ #endif #include #include +#include "device.h" #include "error.h" #include "extension.h" #include "gl.h" @@ -64,7 +65,8 @@ bool is_supported(const string &ext) if(!init_done) { - if(get_backend_api()==OPENGL && get_backend_version()>=Version(3, 0)) + const DeviceInfo &dev_info = Device::get_current().get_info(); + if(dev_info.api==OPENGL && dev_info.api_version>=Version(3, 0)) { typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint); FPtr_glGetStringi glGetStringi = reinterpret_cast(get_proc_address("glGetStringi")); @@ -90,7 +92,7 @@ bool is_supported(const string &ext) bool is_supported(const Version &core_version, const Version &deprecated_version) { - const Version &version = get_backend_version(); + const Version &version = Device::get_current().get_info().api_version; if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE) return false; return (version>=core_version); @@ -119,7 +121,7 @@ bool is_disabled(const string &ext) /* AMD's uniform buffer objects only work with the core version of shaders. */ - if(get_backend_version()=Version(3, 0)) + const DeviceInfo &dev_info = Device::get_current().get_info(); + if(dev_info.api==OPENGL && dev_info.api_version>=Version(3, 0)) { int mask; glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask); diff --git a/source/backends/opengl/pipelinestate_backend.cpp b/source/backends/opengl/pipelinestate_backend.cpp index cda3439c..58e12e7f 100644 --- a/source/backends/opengl/pipelinestate_backend.cpp +++ b/source/backends/opengl/pipelinestate_backend.cpp @@ -8,7 +8,7 @@ #include "blend.h" #include "buffer.h" #include "depthtest.h" -#include "deviceinfo.h" +#include "device.h" #include "framebuffer.h" #include "gl.h" #include "pipelinestate.h" @@ -35,9 +35,9 @@ unsigned OpenGLPipelineState::n_clip_distances = 0; OpenGLPipelineState::OpenGLPipelineState() { if(bound_tex_targets.empty()) - bound_tex_targets.resize(DeviceInfo::get_global().limits.max_texture_bindings); + bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings); if(bound_uniform_blocks.empty()) - bound_uniform_blocks.resize(DeviceInfo::get_global().limits.max_uniform_bindings); + bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings); } OpenGLPipelineState::~OpenGLPipelineState() diff --git a/source/backends/opengl/program_backend.cpp b/source/backends/opengl/program_backend.cpp index 32c6d6e1..132ad1c0 100644 --- a/source/backends/opengl/program_backend.cpp +++ b/source/backends/opengl/program_backend.cpp @@ -12,7 +12,7 @@ #include #include #include -#include "deviceinfo.h" +#include "device.h" #include "error.h" #include "program.h" #include "program_backend.h" @@ -90,7 +90,7 @@ unsigned OpenGLProgram::add_stage(Stage type) void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map &spec_values, TransientData &transient) { - SL::Compiler compiler(DeviceInfo::get_global().glsl_features); + SL::Compiler compiler(Device::get_current().get_info().glsl_features); compiler.set_source(mod.get_prepared_source(), ""); compiler.specialize(spec_values); compiler.compile(SL::Compiler::PROGRAM); diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index dbb675ac..65aeaa13 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "deviceinfo.h" +#include "device.h" #include "error.h" #include "sequence.h" #include "sequencebuilder.h" @@ -167,7 +167,7 @@ Sequence *SequenceBuilder::build(const Framebuffer &fbo) const FrameFormat SequenceBuilder::create_frame_format() const { - unsigned samples = min(tmpl.get_maximum_multisample(), DeviceInfo::get_global().limits.max_samples); + unsigned samples = min(tmpl.get_maximum_multisample(), Device::get_current().get_info().limits.max_samples); if(samples +#include "backend.h" #include "device_backend.h" +#include "glsl/features.h" namespace Msp { namespace GL { +/** +Contains information about various limits imposed by the graphics device. +*/ +struct DeviceLimits +{ + unsigned max_clip_planes = 6; + unsigned max_vertex_attributes = 16; + unsigned max_texture_bindings = 16; + unsigned max_color_attachments = 8; + unsigned max_samples = 4; + unsigned max_uniform_bindings = 24; + unsigned uniform_buffer_alignment = 256; + float max_anisotropy = 1.0f; +}; + +/** +Contains information about a graphics device. +*/ +struct DeviceInfo +{ + GraphicsApi api; + Version api_version; + DeviceLimits limits; + SL::Features glsl_features; +}; + /** Represents a graphics device. An instance must be created to use the library. */ @@ -15,6 +43,8 @@ class Device: public DeviceBackend friend DeviceBackend; private: + DeviceInfo info; + static Device *current; public: @@ -22,6 +52,7 @@ public: ~Device(); using DeviceBackend::get_context; + const DeviceInfo &get_info() const { return info; } static Device &get_current(); }; diff --git a/source/core/deviceinfo.cpp b/source/core/deviceinfo.cpp deleted file mode 100644 index 8400ffdf..00000000 --- a/source/core/deviceinfo.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "deviceinfo.h" - -namespace Msp { -namespace GL { - -const DeviceInfo &DeviceInfo::get_global() -{ - static DeviceInfo info; - return info; -} - -} // namespace GL -} // namespace Msp diff --git a/source/core/deviceinfo.h b/source/core/deviceinfo.h deleted file mode 100644 index a9584119..00000000 --- a/source/core/deviceinfo.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef MSP_GL_DEVICEINFO_H_ -#define MSP_GL_DEVICEINFO_H_ - -#include "glsl/features.h" - -namespace Msp { -namespace GL { - -/** -Contains information about various limits imposed by the graphics device. -*/ -struct Limits -{ - unsigned max_clip_planes = 6; - unsigned max_vertex_attributes = 16; - unsigned max_texture_bindings = 16; - unsigned max_color_attachments = 8; - unsigned max_samples = 4; - unsigned max_uniform_bindings = 24; - unsigned uniform_buffer_alignment = 256; - float max_anisotropy = 1.0f; - - Limits(); -}; - -/** -Contains information about a graphics device. -*/ -struct DeviceInfo -{ - Limits limits; - SL::Features glsl_features; - - DeviceInfo(); - - /** Returns information for the device currently in use. */ - static const DeviceInfo &get_global(); -}; - -} // namespace GL -} // namespace Msp - -#endif diff --git a/source/core/module.cpp b/source/core/module.cpp index 89d41894..15bd750c 100644 --- a/source/core/module.cpp +++ b/source/core/module.cpp @@ -1,6 +1,6 @@ #include #include -#include "deviceinfo.h" +#include "device.h" #include "module.h" #include "resources.h" @@ -69,8 +69,9 @@ void Module::load_source(IO::Base &io, const string &name) SL::Features Module::create_features() const { - const SL::Features &device_features = DeviceInfo::get_global().glsl_features; - SL::Features latest_features = SL::Features::latest(get_backend_api()); + const DeviceInfo &dev_info = Device::get_current().get_info(); + const SL::Features &device_features = dev_info.glsl_features; + SL::Features latest_features = SL::Features::latest(dev_info.api); SL::Features features; features.target_api = latest_features.target_api; features.glsl_version = latest_features.glsl_version; diff --git a/source/core/sampler.cpp b/source/core/sampler.cpp index d45dee54..4da13833 100644 --- a/source/core/sampler.cpp +++ b/source/core/sampler.cpp @@ -1,5 +1,5 @@ #include -#include "deviceinfo.h" +#include "device.h" #include "error.h" #include "sampler.h" @@ -38,7 +38,7 @@ void Sampler::set_max_anisotropy(float a) { if(a<1.0f) throw invalid_argument("Sampler::set_max_anisotropy"); - if(a>DeviceInfo::get_global().limits.max_anisotropy) + if(a>Device::get_current().get_info().limits.max_anisotropy) throw out_of_range("Sampler::set_max_anisotropy"); bool supported = check_anisotropic(a>1.0f); max_anisotropy = a; diff --git a/source/core/texture2dmultisample.cpp b/source/core/texture2dmultisample.cpp index ce35bdb7..8ba52522 100644 --- a/source/core/texture2dmultisample.cpp +++ b/source/core/texture2dmultisample.cpp @@ -1,4 +1,4 @@ -#include "deviceinfo.h" +#include "device.h" #include "error.h" #include "texture2dmultisample.h" @@ -17,7 +17,7 @@ void Texture2DMultisample::storage(PixelFormat fmt, unsigned wd, unsigned ht, un } if(wd==0 || ht==0) throw invalid_argument("Texture2DMultisample::storage"); - if(!sm || sm>DeviceInfo::get_global().limits.max_samples) + if(!sm || sm>Device::get_current().get_info().limits.max_samples) throw invalid_argument("Texture2DMultisample::storage"); set_format(fmt); diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp index 89032b5c..adadf056 100644 --- a/source/core/uniformblock.cpp +++ b/source/core/uniformblock.cpp @@ -1,5 +1,5 @@ #include -#include "deviceinfo.h" +#include "device.h" #include "uniformblock.h" using namespace std; @@ -14,7 +14,7 @@ UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info): size_t UniformBlock::get_alignment() const { - return DeviceInfo::get_global().limits.uniform_buffer_alignment; + return Device::get_current().get_info().limits.uniform_buffer_alignment; } void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size, const void *value) diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index 50f9c183..61bf032c 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -1,4 +1,4 @@ -#include "deviceinfo.h" +#include "device.h" #include "error.h" #include "vertexarray.h" #include "vertexsetup.h" @@ -72,7 +72,7 @@ bool VertexSetup::verify_format(const VertexFormat &fmt) if(fmt.empty()) return false; - static unsigned max_attribs = DeviceInfo::get_global().limits.max_vertex_attributes; + static unsigned max_attribs = Device::get_current().get_info().limits.max_vertex_attributes; return all_of(fmt.begin(), fmt.end(), [](VertexAttribute a){ return get_attribute_semantic(a)