From 2d3113a7dbbe4be2f1d1e8980c1c4e42175163da Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 9 Aug 2021 12:12:06 +0300 Subject: [PATCH] Store implementation limits in a central struct --- source/builders/sequencebuilder.cpp | 3 ++- source/core/buffer.cpp | 9 ++++----- source/core/buffer.h | 4 ++-- source/core/clipping.cpp | 7 +++---- source/core/clipping.h | 2 +- source/core/deviceinfo.cpp | 24 ++++++++++++++++++++++++ source/core/deviceinfo.h | 24 ++++++++++++++++++++++++ source/core/renderbuffer.cpp | 7 +++---- source/core/renderbuffer.h | 2 +- source/core/texunit.cpp | 15 +++------------ source/core/texunit.h | 2 +- source/core/uniformblock.cpp | 3 ++- source/core/vertexsetup.cpp | 7 +++---- source/render/renderer.cpp | 3 ++- 14 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 source/core/deviceinfo.cpp create mode 100644 source/core/deviceinfo.h diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index 6cf34c0e..9bb8f1dc 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "deviceinfo.h" #include "error.h" #include "renderbuffer.h" #include "sequence.h" @@ -52,7 +53,7 @@ void SequenceBuilder::build(Sequence &sequence) const sequence.set_hdr(tmpl.get_hdr()); sequence.set_alpha(tmpl.get_alpha()); - unsigned samples = min(tmpl.get_maximum_multisample(), Renderbuffer::get_max_samples()); + unsigned samples = min(tmpl.get_maximum_multisample(), Limits::get_global().max_samples); if(samples #include #include "buffer.h" +#include "deviceinfo.h" #include "error.h" #include "misc.h" #include "vertexsetup.h" @@ -297,7 +298,7 @@ const BufferRange *&BufferRange::binding(BufferType type, unsigned index) { if(type==UNIFORM_BUFFER) { - if(index>=get_n_uniform_buffer_bindings()) + if(index>=Limits::get_global().max_uniform_bindings) throw out_of_range("BufferRange::binding"); if(bound_uniform.size()<=index) bound_uniform.resize(index+1); @@ -319,14 +320,12 @@ bool BufferRange::set_current(BufferType type, unsigned index, const BufferRange unsigned BufferRange::get_n_uniform_buffer_bindings() { - static unsigned count = get_i(GL_MAX_UNIFORM_BUFFER_BINDINGS); - return count; + return Limits::get_global().max_uniform_bindings; } unsigned BufferRange::get_uniform_buffer_alignment() { - static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT); - return align; + return Limits::get_global().uniform_buffer_alignment; } } // namespace GL diff --git a/source/core/buffer.h b/source/core/buffer.h index 7ef84668..ad308429 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -163,8 +163,8 @@ private: static bool set_current(BufferType, unsigned, const BufferRange *); public: - static unsigned get_n_uniform_buffer_bindings(); - static unsigned get_uniform_buffer_alignment(); + DEPRECATED static unsigned get_n_uniform_buffer_bindings(); + DEPRECATED static unsigned get_uniform_buffer_alignment(); }; } // namespace GL diff --git a/source/core/clipping.cpp b/source/core/clipping.cpp index 18b706e7..644a2434 100644 --- a/source/core/clipping.cpp +++ b/source/core/clipping.cpp @@ -2,6 +2,7 @@ #include #include "clipping.h" #include "clipplane.h" +#include "deviceinfo.h" #include "error.h" #include "matrix.h" #include "misc.h" @@ -13,16 +14,14 @@ namespace GL { unsigned Clipping::get_n_attach_points() { - static Require _req(MSP_clipping); - static int count = get_i(GL_MAX_CLIP_PLANES); - return count; + return Limits::get_global().max_clip_planes; } void Clipping::attach(const ClipPlane &p) { if(find_member(planes, &p, &AttachedPlane::plane)!=planes.end()) return; - if(planes.size()>=get_n_attach_points()) + if(planes.size()>=Limits::get_global().max_clip_planes) throw invalid_operation("Clipping::attach"); planes.push_back(&p); diff --git a/source/core/clipping.h b/source/core/clipping.h index d74763ab..b0719523 100644 --- a/source/core/clipping.h +++ b/source/core/clipping.h @@ -27,7 +27,7 @@ private: mutable ProgramData shdata; public: - static unsigned get_n_attach_points(); + DEPRECATED static unsigned get_n_attach_points(); void attach(const ClipPlane &); void detach(const ClipPlane &); diff --git a/source/core/deviceinfo.cpp b/source/core/deviceinfo.cpp new file mode 100644 index 00000000..54cb88b4 --- /dev/null +++ b/source/core/deviceinfo.cpp @@ -0,0 +1,24 @@ +#include "deviceinfo.h" +#include "gl.h" + +namespace Msp { +namespace GL { + +Limits::Limits() +{ + glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, reinterpret_cast(&max_vertex_attributes)); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, reinterpret_cast(&max_texture_bindings)); + glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, reinterpret_cast(&max_uniform_bindings)); + glGetIntegerv(GL_MAX_CLIP_PLANES, reinterpret_cast(&max_clip_planes)); + glGetIntegerv(GL_MAX_SAMPLES, reinterpret_cast(&max_samples)); + glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, reinterpret_cast(&uniform_buffer_alignment)); +} + +const Limits &Limits::get_global() +{ + static Limits limits; + return limits; +} + +} // namespace GL +} // namespace Msp diff --git a/source/core/deviceinfo.h b/source/core/deviceinfo.h new file mode 100644 index 00000000..9ea239b1 --- /dev/null +++ b/source/core/deviceinfo.h @@ -0,0 +1,24 @@ +#ifndef MSP_GL_DEVICEINFO_H_ +#define MSP_GL_DEVICEINFO_H_ + +namespace Msp { +namespace GL { + +struct Limits +{ + unsigned max_vertex_attributes; + unsigned max_texture_bindings; + unsigned max_uniform_bindings; + unsigned max_clip_planes; + unsigned max_samples; + unsigned uniform_buffer_alignment; + + Limits(); + + static const Limits &get_global(); +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/core/renderbuffer.cpp b/source/core/renderbuffer.cpp index 9af18dae..57b43eba 100644 --- a/source/core/renderbuffer.cpp +++ b/source/core/renderbuffer.cpp @@ -2,7 +2,7 @@ #include #include #include -#include "misc.h" +#include "deviceinfo.h" #include "renderbuffer.h" using namespace std; @@ -41,8 +41,7 @@ void Renderbuffer::storage(PixelFormat fmt, unsigned wd, unsigned ht) unsigned Renderbuffer::get_max_samples() { - static unsigned max_samples = (EXT_framebuffer_multisample ? get_i(GL_MAX_SAMPLES) : 0); - return max_samples; + return Limits::get_global().max_samples; } void Renderbuffer::storage_multisample(unsigned samples, PixelFormat fmt, unsigned wd, unsigned ht) @@ -51,7 +50,7 @@ void Renderbuffer::storage_multisample(unsigned samples, PixelFormat fmt, unsign return storage(fmt, wd, ht); static Require _req(EXT_framebuffer_multisample); - if(samples>get_max_samples()) + if(samples>Limits::get_global().max_samples) throw out_of_range("Renderbuffer::storage_multisample"); require_pixelformat(fmt); diff --git a/source/core/renderbuffer.h b/source/core/renderbuffer.h index aea23080..4462e31c 100644 --- a/source/core/renderbuffer.h +++ b/source/core/renderbuffer.h @@ -36,7 +36,7 @@ public: /** Returns the maximum supported sample count for multisampling. If multisampling is not supported, returns 0. */ - static unsigned get_max_samples(); + DEPRECATED static unsigned get_max_samples(); /** Allocates multisample storage for the renderbuffer. All attachments in a framebuffer must have the same number of samples. To transfer the diff --git a/source/core/texunit.cpp b/source/core/texunit.cpp index 0659d615..7405d8da 100644 --- a/source/core/texunit.cpp +++ b/source/core/texunit.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "deviceinfo.h" #include "gl.h" #include "misc.h" #include "texture.h" @@ -42,24 +43,14 @@ void TexUnit::bind() unsigned TexUnit::get_n_units() { - static int count = -1; - if(count<0) - { - if(ARB_vertex_shader) - count = get_i(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS); - else if(ARB_multitexture) - count = get_i(GL_MAX_TEXTURE_UNITS); - else - count = 1; - } - return count; + return Limits::get_global().max_texture_bindings; } TexUnit &TexUnit::get_unit(unsigned n) { if(n>0) static Require _req(ARB_multitexture); - if(n>=get_n_units()) + if(n>=Limits::get_global().max_texture_bindings) throw out_of_range("TexUnit::get_unit"); if(units.size()<=n) diff --git a/source/core/texunit.h b/source/core/texunit.h index 6253d8d9..8fefc3a8 100644 --- a/source/core/texunit.h +++ b/source/core/texunit.h @@ -32,7 +32,7 @@ public: const Sampler *get_sampler() const { return sampler; } void bind(); - static unsigned get_n_units(); + DEPRECATED static unsigned get_n_units(); static TexUnit &get_unit(unsigned); static TexUnit ¤t(); static TexUnit *find_unit(const Texture *); diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp index f114ea58..59871cdd 100644 --- a/source/core/uniformblock.cpp +++ b/source/core/uniformblock.cpp @@ -3,6 +3,7 @@ #include #include "buffer.h" #include "color.h" +#include "deviceinfo.h" #include "error.h" #include "matrix.h" #include "uniform.h" @@ -67,7 +68,7 @@ BufferBackedUniformBlock::~BufferBackedUniformBlock() unsigned BufferBackedUniformBlock::get_alignment() const { - return BufferRange::get_uniform_buffer_alignment(); + return Limits::get_global().uniform_buffer_alignment; } void BufferBackedUniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index da79e7f6..1094bb21 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -7,6 +7,7 @@ #include #include #include "buffer.h" +#include "deviceinfo.h" #include "error.h" #include "gl.h" #include "misc.h" @@ -98,12 +99,10 @@ bool VertexSetup::verify_format(const VertexFormat &fmt) if(fmt.empty()) return false; - static int max_attribs = -1; - if(max_attribs<0) - max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS); + unsigned max_attribs = Limits::get_global().max_vertex_attributes; for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) - if(static_cast(get_attribute_semantic(*a))>=max_attribs) + if(get_attribute_semantic(*a)>=max_attribs) return false; return true; diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 8de24adb..a8f5ce3a 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -2,6 +2,7 @@ #include "buffer.h" #include "camera.h" #include "clipping.h" +#include "deviceinfo.h" #include "error.h" #include "lighting.h" #include "material.h" @@ -401,7 +402,7 @@ Renderer::BoundProgramData::BoundProgramData(const ProgramData *d): Renderer::State::State(): camera(0), texture_count(0), - lowest_effect_texunit(TexUnit::get_n_units()), + lowest_effect_texunit(Limits::get_global().max_texture_bindings), clipping(0), shprog(0), shdata_count(0), -- 2.43.0